2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-07-09 16:31: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-07-09 16:31: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 of the sandbox2::StackTrace class.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/stack_trace.h"
|
|
|
|
|
2023-01-19 21:44:11 +08:00
|
|
|
#include <fcntl.h>
|
2019-07-09 16:31:48 +08:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <syscall.h>
|
2019-10-23 00:05:13 +08:00
|
|
|
|
2019-07-09 16:31:48 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
#include "absl/cleanup/cleanup.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/flags/flag.h"
|
|
|
|
#include "absl/log/log.h"
|
2019-07-09 16:31:48 +08:00
|
|
|
#include "absl/memory/memory.h"
|
2021-06-28 17:02:35 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-07-09 16:31:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "absl/strings/strip.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2019-07-09 16:31:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/limits.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policybuilder.h"
|
|
|
|
#include "sandboxed_api/sandbox2/regs.h"
|
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
|
|
|
#include "sandboxed_api/sandbox2/unwind/unwind.h"
|
|
|
|
#include "sandboxed_api/sandbox2/unwind/unwind.pb.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
|
|
|
#include "sandboxed_api/util/path.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
2021-06-28 17:02:35 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
ABSL_FLAG(bool, sandbox_disable_all_stack_traces, false,
|
|
|
|
"Completely disable stack trace collection for sandboxees");
|
|
|
|
|
|
|
|
ABSL_FLAG(bool, sandbox_libunwind_crash_handler, true,
|
|
|
|
"Sandbox libunwind when handling violations (preferred)");
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
2021-06-28 17:02:35 +08:00
|
|
|
namespace {
|
2019-07-09 16:31:48 +08:00
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace file = ::sapi::file;
|
|
|
|
namespace file_util = ::sapi::file_util;
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
// Similar to GetStackTrace() but without using the sandbox to isolate
|
|
|
|
// libunwind.
|
|
|
|
absl::StatusOr<std::vector<std::string>> UnsafeGetStackTrace(pid_t pid) {
|
|
|
|
LOG(WARNING) << "Using non-sandboxed libunwind";
|
|
|
|
return RunLibUnwindAndSymbolizer(pid, kDefaultMaxFrames);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-07-09 16:31:48 +08:00
|
|
|
class StackTracePeer {
|
|
|
|
public:
|
2021-06-28 17:02:35 +08:00
|
|
|
static absl::StatusOr<std::unique_ptr<Policy>> GetPolicy(
|
|
|
|
pid_t target_pid, const std::string& maps_file,
|
|
|
|
const std::string& app_path, const std::string& exe_path,
|
2023-01-19 18:47:17 +08:00
|
|
|
const Namespace* ns, bool uses_custom_forkserver);
|
2021-06-28 17:02:35 +08:00
|
|
|
|
2023-02-16 21:06:50 +08:00
|
|
|
static absl::StatusOr<std::vector<std::string>> LaunchLibunwindSandbox(
|
2023-01-19 18:47:17 +08:00
|
|
|
const Regs* regs, const Namespace* ns, bool uses_custom_forkserver);
|
2019-07-09 16:31:48 +08:00
|
|
|
};
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
absl::StatusOr<std::unique_ptr<Policy>> StackTracePeer::GetPolicy(
|
|
|
|
pid_t target_pid, const std::string& maps_file, const std::string& app_path,
|
2023-01-19 18:47:17 +08:00
|
|
|
const std::string& exe_path, const Namespace* ns,
|
|
|
|
bool uses_custom_forkserver) {
|
2019-07-09 16:31:48 +08:00
|
|
|
PolicyBuilder builder;
|
2023-01-19 18:47:17 +08:00
|
|
|
if (uses_custom_forkserver) {
|
|
|
|
// Custom forkserver just forks, the binary is loaded outside of the
|
|
|
|
// sandboxee's mount namespace.
|
|
|
|
// Add all possible libraries without the need of parsing the binary
|
|
|
|
// or /proc/pid/maps.
|
|
|
|
for (const auto& library_path : {
|
|
|
|
"/usr/lib64",
|
|
|
|
"/usr/lib",
|
|
|
|
"/lib64",
|
|
|
|
"/lib",
|
|
|
|
}) {
|
|
|
|
if (access(library_path, F_OK) != -1) {
|
|
|
|
VLOG(1) << "Adding library folder '" << library_path << "'";
|
|
|
|
builder.AddDirectory(library_path);
|
|
|
|
} else {
|
|
|
|
VLOG(1) << "Could not add library folder '" << library_path
|
|
|
|
<< "' as it does not exist";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use the mounttree of the original executable.
|
|
|
|
CHECK(ns != nullptr);
|
2023-01-19 21:44:11 +08:00
|
|
|
Mounts mounts = ns->mounts();
|
|
|
|
mounts.Remove("/proc").IgnoreError();
|
|
|
|
mounts.Remove(app_path).IgnoreError();
|
|
|
|
builder.SetMounts(std::move(mounts));
|
2023-01-19 18:47:17 +08:00
|
|
|
}
|
|
|
|
builder.AllowOpen()
|
2019-07-09 16:31:48 +08:00
|
|
|
.AllowRead()
|
|
|
|
.AllowWrite()
|
|
|
|
.AllowSyscall(__NR_close)
|
|
|
|
.AllowExit()
|
|
|
|
.AllowHandleSignals()
|
2023-01-19 18:47:17 +08:00
|
|
|
.AllowTcMalloc()
|
|
|
|
.AllowSystemMalloc()
|
2023-01-19 21:44:11 +08:00
|
|
|
// for Comms:RecvFD
|
|
|
|
.AllowSyscall(__NR_recvmsg)
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
// libunwind
|
2023-01-19 18:47:17 +08:00
|
|
|
.AllowMmap()
|
2021-12-07 02:22:45 +08:00
|
|
|
.AllowStat()
|
2019-07-09 16:31:48 +08:00
|
|
|
.AllowSyscall(__NR_lseek)
|
2020-09-14 16:18:09 +08:00
|
|
|
#ifdef __NR__llseek
|
|
|
|
.AllowSyscall(__NR__llseek) // Newer glibc on PPC
|
|
|
|
#endif
|
2019-07-09 16:31:48 +08:00
|
|
|
.AllowSyscall(__NR_mincore)
|
|
|
|
.AllowSyscall(__NR_munmap)
|
2023-01-19 18:47:17 +08:00
|
|
|
.AllowPipe()
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
// Symbolizer
|
|
|
|
.AllowSyscall(__NR_brk)
|
2023-01-19 18:47:17 +08:00
|
|
|
.AllowTime()
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
// Other
|
2023-01-19 18:47:17 +08:00
|
|
|
.AllowDup()
|
|
|
|
.AllowSafeFcntl()
|
|
|
|
.AllowGetPIDs()
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
// Required for our ptrace replacement.
|
2022-11-30 21:59:36 +08:00
|
|
|
.TrapPtrace()
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
// Add proc maps.
|
|
|
|
.AddFileAt(maps_file,
|
|
|
|
file::JoinPath("/proc", absl::StrCat(target_pid), "maps"))
|
|
|
|
.AddFileAt(maps_file,
|
|
|
|
file::JoinPath("/proc", absl::StrCat(target_pid), "task",
|
|
|
|
absl::StrCat(target_pid), "maps"))
|
|
|
|
|
|
|
|
// Add the binary itself.
|
|
|
|
.AddFileAt(exe_path, app_path);
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(std::unique_ptr<Policy> policy, builder.TryBuild());
|
|
|
|
return std::move(policy);
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-02-16 21:06:50 +08:00
|
|
|
namespace internal {
|
|
|
|
SandboxPeer::SpawnFn SandboxPeer::spawn_fn_ = nullptr;
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
absl::StatusOr<std::vector<std::string>> StackTracePeer::LaunchLibunwindSandbox(
|
2023-01-19 18:47:17 +08:00
|
|
|
const Regs* regs, const Namespace* ns, bool uses_custom_forkserver) {
|
2019-07-09 16:31:48 +08:00
|
|
|
const pid_t pid = regs->pid();
|
|
|
|
|
2023-01-19 21:44:11 +08:00
|
|
|
sapi::file_util::fileops::FDCloser memory_fd(
|
|
|
|
open(absl::StrCat("/proc/", pid, "/mem").c_str(), O_RDONLY));
|
|
|
|
if (memory_fd.get() == -1) {
|
|
|
|
return absl::InternalError("Opening sandboxee process memory failed");
|
|
|
|
}
|
2022-10-25 21:20:08 +08:00
|
|
|
// Tell executor to use this special internal mode. Using `new` to access a
|
|
|
|
// non-public constructor.
|
2019-07-09 16:31:48 +08:00
|
|
|
auto executor = absl::WrapUnique(new Executor(pid));
|
|
|
|
|
2023-03-03 17:13:44 +08:00
|
|
|
executor->limits()->set_rlimit_cpu(10).set_walltime_limit(absl::Seconds(5));
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
// Temporary directory used to provide files from /proc to the unwind sandbox.
|
|
|
|
char unwind_temp_directory_template[] = "/tmp/.sandbox2_unwind_XXXXXX";
|
|
|
|
char* unwind_temp_directory = mkdtemp(unwind_temp_directory_template);
|
|
|
|
if (!unwind_temp_directory) {
|
2021-06-28 17:02:35 +08:00
|
|
|
return absl::InternalError(
|
|
|
|
"Could not create temporary directory for unwinding");
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
struct UnwindTempDirectoryCleanup {
|
|
|
|
~UnwindTempDirectoryCleanup() {
|
|
|
|
file_util::fileops::DeleteRecursively(capture);
|
|
|
|
}
|
|
|
|
char* capture;
|
|
|
|
} cleanup{unwind_temp_directory};
|
|
|
|
|
|
|
|
// Copy over important files from the /proc directory as we can't mount them.
|
|
|
|
const std::string unwind_temp_maps_path =
|
|
|
|
file::JoinPath(unwind_temp_directory, "maps");
|
|
|
|
|
|
|
|
if (!file_util::fileops::CopyFile(
|
|
|
|
file::JoinPath("/proc", absl::StrCat(pid), "maps"),
|
|
|
|
unwind_temp_maps_path, 0400)) {
|
2021-06-28 17:02:35 +08:00
|
|
|
return absl::InternalError("Could not copy maps file");
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get path to the binary.
|
2019-10-23 00:05:13 +08:00
|
|
|
// app_path contains the path like it is also in /proc/pid/maps. It is
|
|
|
|
// relative to the sandboxee's mount namespace. If it is not existing
|
|
|
|
// (anymore) it will have a ' (deleted)' suffix.
|
2019-07-09 16:31:48 +08:00
|
|
|
std::string app_path;
|
|
|
|
std::string proc_pid_exe = file::JoinPath("/proc", absl::StrCat(pid), "exe");
|
|
|
|
if (!file_util::fileops::ReadLinkAbsolute(proc_pid_exe, &app_path)) {
|
2021-06-28 17:02:35 +08:00
|
|
|
return absl::InternalError("Could not obtain absolute path to the binary");
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
2019-10-23 00:05:13 +08:00
|
|
|
// The exe_path will have a mountable path of the application, even if it was
|
|
|
|
// removed.
|
|
|
|
// Resolve app_path backing file.
|
2023-01-19 18:47:17 +08:00
|
|
|
std::string exe_path =
|
|
|
|
ns ? ns->mounts().ResolvePath(app_path).value_or("") : "";
|
2019-10-23 00:05:13 +08:00
|
|
|
|
|
|
|
if (exe_path.empty()) {
|
|
|
|
// File was probably removed.
|
2019-07-09 16:31:48 +08:00
|
|
|
LOG(WARNING) << "File was removed, using /proc/pid/exe.";
|
|
|
|
app_path = std::string(absl::StripSuffix(app_path, " (deleted)"));
|
|
|
|
// Create a copy of /proc/pid/exe, mount that one.
|
|
|
|
exe_path = file::JoinPath(unwind_temp_directory, "exe");
|
|
|
|
if (!file_util::fileops::CopyFile(proc_pid_exe, exe_path, 0700)) {
|
2021-06-28 17:02:35 +08:00
|
|
|
return absl::InternalError("Could not copy /proc/pid/exe");
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(1) << "Resolved binary: " << app_path << " / " << exe_path;
|
|
|
|
|
|
|
|
// Add mappings for the binary (as they might not have been added due to the
|
|
|
|
// forkserver).
|
2023-01-19 18:47:17 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
std::unique_ptr<Policy> policy,
|
|
|
|
StackTracePeer::GetPolicy(pid, unwind_temp_maps_path, app_path, exe_path,
|
|
|
|
ns, uses_custom_forkserver));
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
VLOG(1) << "Running libunwind sandbox";
|
2023-02-16 21:06:50 +08:00
|
|
|
auto sandbox =
|
|
|
|
internal::SandboxPeer::Spawn(std::move(executor), std::move(policy));
|
|
|
|
Comms* comms = sandbox->comms();
|
2020-07-20 15:24:12 +08:00
|
|
|
|
2019-07-09 16:31:48 +08:00
|
|
|
UnwindSetup msg;
|
|
|
|
msg.set_pid(pid);
|
|
|
|
msg.set_regs(reinterpret_cast<const char*>(®s->user_regs_),
|
|
|
|
sizeof(regs->user_regs_));
|
|
|
|
msg.set_default_max_frames(kDefaultMaxFrames);
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
absl::Cleanup kill_sandbox = [&sandbox]() {
|
2023-02-16 21:06:50 +08:00
|
|
|
sandbox->Kill();
|
|
|
|
sandbox->AwaitResult().IgnoreResult();
|
2021-06-28 17:02:35 +08:00
|
|
|
};
|
|
|
|
|
2019-07-09 16:31:48 +08:00
|
|
|
if (!comms->SendProtoBuf(msg)) {
|
2021-06-28 17:02:35 +08:00
|
|
|
return absl::InternalError("Sending libunwind setup message failed");
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
2023-01-19 21:44:11 +08:00
|
|
|
if (!comms->SendFD(memory_fd.get())) {
|
|
|
|
return absl::InternalError("Sending sandboxee's memory fd failed");
|
|
|
|
}
|
2021-06-28 17:02:35 +08:00
|
|
|
absl::Status status;
|
|
|
|
if (!comms->RecvStatus(&status)) {
|
|
|
|
return absl::InternalError(
|
|
|
|
"Receiving status from libunwind sandbox failed");
|
2021-06-23 05:49:54 +08:00
|
|
|
}
|
2022-10-25 21:20:08 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(status);
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
UnwindResult result;
|
|
|
|
if (!comms->RecvProtoBuf(&result)) {
|
|
|
|
return absl::InternalError("Receiving libunwind result failed");
|
2021-06-23 05:49:54 +08:00
|
|
|
}
|
2021-06-28 17:02:35 +08:00
|
|
|
|
|
|
|
std::move(kill_sandbox).Cancel();
|
|
|
|
|
2023-02-16 21:06:50 +08:00
|
|
|
auto sandbox_result = sandbox->AwaitResult();
|
2019-07-09 16:31:48 +08:00
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
LOG(INFO) << "Libunwind execution status: " << sandbox_result.ToString();
|
2019-07-09 16:31:48 +08:00
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
if (sandbox_result.final_status() != Result::OK) {
|
|
|
|
return absl::InternalError(
|
|
|
|
absl::StrCat("libunwind sandbox did not finish properly: ",
|
|
|
|
sandbox_result.ToString()));
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:06:50 +08:00
|
|
|
return std::vector<std::string>(result.stacktrace().begin(),
|
|
|
|
result.stacktrace().end());
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-01-19 18:47:17 +08:00
|
|
|
absl::StatusOr<std::vector<std::string>> GetStackTrace(
|
|
|
|
const Regs* regs, const Namespace* ns, bool uses_custom_forkserver) {
|
2019-07-09 16:31:48 +08:00
|
|
|
if (absl::GetFlag(FLAGS_sandbox_disable_all_stack_traces)) {
|
2021-06-28 17:02:35 +08:00
|
|
|
return absl::UnavailableError("Stacktraces disabled");
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
if (!regs) {
|
2021-06-28 17:02:35 +08:00
|
|
|
return absl::InvalidArgumentError(
|
|
|
|
"Could not obtain stacktrace, regs == nullptr");
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show a warning if sandboxed libunwind is requested but we're running in
|
2021-02-01 23:10:43 +08:00
|
|
|
// an ASAN/coverage build (= we can't use sandboxed libunwind).
|
|
|
|
if (const bool coverage_enabled =
|
|
|
|
getenv("COVERAGE") != nullptr;
|
|
|
|
absl::GetFlag(FLAGS_sandbox_libunwind_crash_handler) &&
|
|
|
|
(sapi::sanitizers::IsAny() || coverage_enabled)) {
|
|
|
|
LOG_IF(WARNING, sapi::sanitizers::IsAny())
|
2019-07-09 16:31:48 +08:00
|
|
|
<< "Sanitizer build, using non-sandboxed libunwind";
|
|
|
|
LOG_IF(WARNING, coverage_enabled)
|
|
|
|
<< "Coverage build, using non-sandboxed libunwind";
|
2020-07-20 15:24:12 +08:00
|
|
|
return UnsafeGetStackTrace(regs->pid());
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!absl::GetFlag(FLAGS_sandbox_libunwind_crash_handler)) {
|
2020-07-20 15:24:12 +08:00
|
|
|
return UnsafeGetStackTrace(regs->pid());
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-02-16 21:06:50 +08:00
|
|
|
return StackTracePeer::LaunchLibunwindSandbox(regs, ns,
|
|
|
|
uses_custom_forkserver);
|
2020-07-20 15:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> CompactStackTrace(
|
|
|
|
const std::vector<std::string>& stack_trace) {
|
|
|
|
std::vector<std::string> compact_trace;
|
|
|
|
compact_trace.reserve(stack_trace.size() / 2);
|
|
|
|
const std::string* prev = nullptr;
|
|
|
|
int seen = 0;
|
|
|
|
auto add_repeats = [&compact_trace](int seen) {
|
|
|
|
if (seen != 0) {
|
|
|
|
compact_trace.push_back(
|
|
|
|
absl::StrCat("(previous frame repeated ", seen, " times)"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (const auto& frame : stack_trace) {
|
|
|
|
if (prev && frame == *prev) {
|
|
|
|
++seen;
|
|
|
|
} else {
|
|
|
|
prev = &frame;
|
|
|
|
add_repeats(seen);
|
|
|
|
seen = 0;
|
|
|
|
compact_trace.push_back(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_repeats(seen);
|
|
|
|
return compact_trace;
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|