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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/stack_trace.h"
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <sys/types.h>
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
#include <cstdio>
|
2022-02-16 21:58:43 +08:00
|
|
|
#include <functional>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2022-02-16 21:58:43 +08:00
|
|
|
#include <string>
|
2019-07-09 16:31:48 +08:00
|
|
|
#include <utility>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <vector>
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2023-09-19 21:49:17 +08:00
|
|
|
#include "absl/base/log_severity.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/flags/declare.h"
|
|
|
|
#include "absl/flags/flag.h"
|
|
|
|
#include "absl/flags/reflection.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/log/check.h"
|
2023-09-19 21:49:17 +08:00
|
|
|
#include "absl/log/scoped_mock_log.h"
|
2019-07-09 16:31:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2023-03-02 17:25:02 +08:00
|
|
|
#include "absl/time/time.h"
|
2019-07-09 16:31:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/global_forkclient.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policybuilder.h"
|
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/testing.h"
|
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2019-07-09 16:31:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
|
|
|
ABSL_DECLARE_FLAG(bool, sandbox_libunwind_crash_handler);
|
|
|
|
|
2020-04-02 22:42:17 +08:00
|
|
|
namespace sandbox2 {
|
2023-09-19 21:49:17 +08:00
|
|
|
|
|
|
|
class StackTraceTestPeer {
|
|
|
|
public:
|
|
|
|
static StackTraceTestPeer& GetInstance() {
|
|
|
|
static auto* peer = new StackTraceTestPeer();
|
|
|
|
return *peer;
|
|
|
|
}
|
|
|
|
std::unique_ptr<internal::SandboxPeer> SpawnFn(
|
|
|
|
std::unique_ptr<Executor> executor, std::unique_ptr<Policy> policy) {
|
|
|
|
if (crash_unwind_) {
|
|
|
|
policy = PolicyBuilder().BuildOrDie();
|
|
|
|
crash_unwind_ = false;
|
|
|
|
}
|
|
|
|
return old_spawn_fn_(std::move(executor), std::move(policy));
|
|
|
|
}
|
|
|
|
void ReplaceSpawnFn() {
|
|
|
|
old_spawn_fn_ = internal::SandboxPeer::spawn_fn_;
|
|
|
|
internal::SandboxPeer::spawn_fn_ = +[](std::unique_ptr<Executor> executor,
|
|
|
|
std::unique_ptr<Policy> policy) {
|
|
|
|
return GetInstance().SpawnFn(std::move(executor), std::move(policy));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
void RestoreSpawnFn() { internal::SandboxPeer::spawn_fn_ = old_spawn_fn_; }
|
|
|
|
void CrashNextUnwind() { crash_unwind_ = true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
internal::SandboxPeer::SpawnFn old_spawn_fn_;
|
|
|
|
bool crash_unwind_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ScopedSpawnOverride {
|
|
|
|
ScopedSpawnOverride() { StackTraceTestPeer::GetInstance().ReplaceSpawnFn(); }
|
|
|
|
~ScopedSpawnOverride() { StackTraceTestPeer::GetInstance().RestoreSpawnFn(); }
|
|
|
|
ScopedSpawnOverride(ScopedSpawnOverride&&) = delete;
|
|
|
|
ScopedSpawnOverride& operator=(ScopedSpawnOverride&&) = delete;
|
|
|
|
ScopedSpawnOverride(const ScopedSpawnOverride&) = delete;
|
|
|
|
ScopedSpawnOverride& operator=(const ScopedSpawnOverride&) = delete;
|
|
|
|
|
|
|
|
void CrashNextUnwind() {
|
|
|
|
StackTraceTestPeer::GetInstance().CrashNextUnwind();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-02 22:42:17 +08:00
|
|
|
namespace {
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace file_util = ::sapi::file_util;
|
2023-03-03 22:50:17 +08:00
|
|
|
using ::sapi::CreateDefaultPermissiveTestPolicy;
|
2021-01-14 01:25:25 +08:00
|
|
|
using ::sapi::GetTestSourcePath;
|
2023-09-19 21:49:17 +08:00
|
|
|
using ::testing::_;
|
2023-03-02 17:25:02 +08:00
|
|
|
using ::testing::Contains;
|
2023-09-19 21:49:17 +08:00
|
|
|
using ::testing::ContainsRegex;
|
2020-07-20 15:24:12 +08:00
|
|
|
using ::testing::ElementsAre;
|
2019-07-09 16:31:48 +08:00
|
|
|
using ::testing::Eq;
|
2020-07-20 15:24:12 +08:00
|
|
|
using ::testing::IsEmpty;
|
2023-03-02 17:25:02 +08:00
|
|
|
using ::testing::StartsWith;
|
|
|
|
|
|
|
|
struct TestCase {
|
2023-03-02 21:09:05 +08:00
|
|
|
std::string testname = "CrashMe";
|
|
|
|
int testno = 1;
|
|
|
|
int testmode = 1;
|
2023-03-02 17:25:02 +08:00
|
|
|
int final_status = Result::SIGNALED;
|
2023-03-02 21:09:05 +08:00
|
|
|
std::string function_name = testname;
|
2023-03-02 17:25:02 +08:00
|
|
|
std::string full_function_description = "CrashMe(char)";
|
|
|
|
std::function<void(PolicyBuilder*)> modify_policy;
|
|
|
|
absl::Duration wall_time_limit = absl::ZeroDuration();
|
|
|
|
};
|
|
|
|
|
|
|
|
class StackTraceTest : public ::testing::TestWithParam<TestCase> {};
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
// Test that symbolization of stack traces works.
|
2023-03-02 17:25:02 +08:00
|
|
|
void SymbolizationWorksCommon(TestCase param) {
|
2019-07-09 16:31:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/symbolize");
|
2023-03-02 21:09:05 +08:00
|
|
|
std::vector<std::string> args = {path, absl::StrCat(param.testno),
|
|
|
|
absl::StrCat(param.testmode)};
|
2022-02-16 21:58:43 +08:00
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
PolicyBuilder builder = CreateDefaultPermissiveTestPolicy(path);
|
2023-03-02 17:25:02 +08:00
|
|
|
if (param.modify_policy) {
|
2023-03-03 22:50:17 +08:00
|
|
|
param.modify_policy(&builder);
|
2023-02-16 23:11:48 +08:00
|
|
|
}
|
2023-03-03 22:50:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, builder.TryBuild());
|
2019-07-09 16:31:48 +08:00
|
|
|
|
2022-10-12 20:22:51 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2023-03-02 17:25:02 +08:00
|
|
|
ASSERT_TRUE(s2.RunAsync());
|
|
|
|
s2.set_walltime_limit(param.wall_time_limit);
|
|
|
|
auto result = s2.AwaitResult();
|
2019-07-09 16:31:48 +08:00
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
EXPECT_THAT(result.final_status(), Eq(param.final_status));
|
|
|
|
EXPECT_THAT(result.stack_trace(), Contains(StartsWith(param.function_name)));
|
2019-07-09 16:31:48 +08:00
|
|
|
// Check that demangling works as well.
|
2023-03-02 17:25:02 +08:00
|
|
|
EXPECT_THAT(result.stack_trace(),
|
|
|
|
Contains(StartsWith(param.full_function_description)));
|
2023-03-02 21:09:05 +08:00
|
|
|
EXPECT_THAT(result.stack_trace(), Contains(StartsWith("RunTest")));
|
|
|
|
EXPECT_THAT(result.stack_trace(), Contains(StartsWith("main")));
|
|
|
|
if (param.testmode == 2) {
|
|
|
|
EXPECT_THAT(result.stack_trace(),
|
|
|
|
Contains(StartsWith("RecurseA")).Times(5));
|
|
|
|
EXPECT_THAT(result.stack_trace(),
|
|
|
|
Contains(StartsWith("RecurseB")).Times(5));
|
|
|
|
} else if (param.testmode == 3) {
|
2023-03-06 16:21:29 +08:00
|
|
|
EXPECT_THAT(result.stack_trace(), Contains(StartsWith("LibCallCallback")));
|
2023-03-02 21:09:05 +08:00
|
|
|
EXPECT_THAT(result.stack_trace(), Contains(StartsWith("LibRecurse")));
|
|
|
|
EXPECT_THAT(result.stack_trace(),
|
|
|
|
Contains(StartsWith("LibRecurseA")).Times(5));
|
|
|
|
EXPECT_THAT(result.stack_trace(),
|
|
|
|
Contains(StartsWith("LibRecurseB")).Times(5));
|
|
|
|
}
|
2023-03-02 17:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolizationWorksWithModifiedPolicy(
|
|
|
|
std::function<void(PolicyBuilder*)> modify_policy) {
|
|
|
|
TestCase test_case;
|
|
|
|
test_case.modify_policy = std::move(modify_policy);
|
|
|
|
SymbolizationWorksCommon(test_case);
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
TEST_P(StackTraceTest, SymbolizationWorksNonSandboxedLibunwind) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::FlagSaver fs;
|
|
|
|
absl::SetFlag(&FLAGS_sandbox_libunwind_crash_handler, false);
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksCommon(GetParam());
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
TEST_P(StackTraceTest, SymbolizationWorksSandboxedLibunwind) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::FlagSaver fs;
|
|
|
|
absl::SetFlag(&FLAGS_sandbox_libunwind_crash_handler, true);
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksCommon(GetParam());
|
2019-07-09 16:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StackTraceTest, SymbolizationWorksSandboxedLibunwindProcDirMounted) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::FlagSaver fs;
|
|
|
|
absl::SetFlag(&FLAGS_sandbox_libunwind_crash_handler, true);
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksWithModifiedPolicy(
|
2019-07-09 16:31:48 +08:00
|
|
|
[](PolicyBuilder* builder) { builder->AddDirectory("/proc"); });
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StackTraceTest, SymbolizationWorksSandboxedLibunwindProcFileMounted) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::FlagSaver fs;
|
|
|
|
absl::SetFlag(&FLAGS_sandbox_libunwind_crash_handler, true);
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksWithModifiedPolicy([](PolicyBuilder* builder) {
|
2019-07-09 16:31:48 +08:00
|
|
|
builder->AddFile("/proc/sys/vm/overcommit_memory");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StackTraceTest, SymbolizationWorksSandboxedLibunwindSysDirMounted) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::FlagSaver fs;
|
|
|
|
absl::SetFlag(&FLAGS_sandbox_libunwind_crash_handler, true);
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksWithModifiedPolicy(
|
2019-07-09 16:31:48 +08:00
|
|
|
[](PolicyBuilder* builder) { builder->AddDirectory("/sys"); });
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StackTraceTest, SymbolizationWorksSandboxedLibunwindSysFileMounted) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::FlagSaver fs;
|
|
|
|
absl::SetFlag(&FLAGS_sandbox_libunwind_crash_handler, true);
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksWithModifiedPolicy([](PolicyBuilder* builder) {
|
2019-07-09 16:31:48 +08:00
|
|
|
builder->AddFile("/sys/devices/system/cpu/online");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
size_t FileCountInDirectory(const std::string& path) {
|
2019-07-09 16:31:48 +08:00
|
|
|
std::vector<std::string> fds;
|
|
|
|
std::string error;
|
|
|
|
CHECK(file_util::fileops::ListDirectoryEntries(path, &fds, &error));
|
|
|
|
return fds.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StackTraceTest, ForkEnterNsLibunwindDoesNotLeakFDs) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::FlagSaver fs;
|
|
|
|
absl::SetFlag(&FLAGS_sandbox_libunwind_crash_handler, true);
|
2021-03-18 20:56:15 +08:00
|
|
|
|
|
|
|
// Very first sanitization might create some fds (e.g. for initial
|
|
|
|
// namespaces).
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksCommon({});
|
2021-03-18 20:56:15 +08:00
|
|
|
|
2019-07-09 16:31:48 +08:00
|
|
|
// Get list of open FDs in the global forkserver.
|
2020-11-06 00:47:34 +08:00
|
|
|
pid_t forkserver_pid = GlobalForkClient::GetPid();
|
2019-07-09 16:31:48 +08:00
|
|
|
std::string forkserver_fd_path =
|
|
|
|
absl::StrCat("/proc/", forkserver_pid, "/fd");
|
|
|
|
size_t filecount_before = FileCountInDirectory(forkserver_fd_path);
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
SymbolizationWorksCommon({});
|
2019-07-09 16:31:48 +08:00
|
|
|
|
|
|
|
EXPECT_THAT(filecount_before, Eq(FileCountInDirectory(forkserver_fd_path)));
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
TEST(StackTraceTest, CompactStackTrace) {
|
|
|
|
EXPECT_THAT(CompactStackTrace({}), IsEmpty());
|
|
|
|
EXPECT_THAT(CompactStackTrace({"_start"}), ElementsAre("_start"));
|
|
|
|
EXPECT_THAT(CompactStackTrace({
|
|
|
|
"_start",
|
|
|
|
"main",
|
|
|
|
"recursive_call",
|
|
|
|
"recursive_call",
|
|
|
|
"recursive_call",
|
|
|
|
"tail_call",
|
|
|
|
}),
|
|
|
|
ElementsAre("_start", "main", "recursive_call",
|
|
|
|
"(previous frame repeated 2 times)", "tail_call"));
|
|
|
|
EXPECT_THAT(CompactStackTrace({
|
|
|
|
"_start",
|
|
|
|
"main",
|
|
|
|
"recursive_call",
|
|
|
|
"recursive_call",
|
|
|
|
"recursive_call",
|
|
|
|
"recursive_call",
|
|
|
|
}),
|
|
|
|
ElementsAre("_start", "main", "recursive_call",
|
|
|
|
"(previous frame repeated 3 times)"));
|
|
|
|
}
|
|
|
|
|
2023-09-19 21:49:17 +08:00
|
|
|
TEST(StackTraceTest, RecursiveStackTrace) {
|
|
|
|
// Very first sandbox run will initialize spawn_fn_
|
|
|
|
SKIP_SANITIZERS;
|
|
|
|
ScopedSpawnOverride spawn_override;
|
|
|
|
SymbolizationWorksCommon({});
|
|
|
|
absl::ScopedMockLog log;
|
|
|
|
EXPECT_CALL(
|
|
|
|
log,
|
|
|
|
Log(absl::LogSeverity::kInfo, _,
|
|
|
|
ContainsRegex(
|
|
|
|
"Libunwind execution status: SYSCALL VIOLATION.*Stack: \\w+")));
|
|
|
|
spawn_override.CrashNextUnwind();
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/symbolize");
|
|
|
|
std::vector<std::string> args = {path, absl::StrCat(1), absl::StrCat(1)};
|
|
|
|
PolicyBuilder builder = CreateDefaultPermissiveTestPolicy(path);
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, builder.TryBuild());
|
|
|
|
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
|
|
|
log.StartCapturingLogs();
|
|
|
|
ASSERT_TRUE(s2.RunAsync());
|
|
|
|
auto result = s2.AwaitResult();
|
|
|
|
EXPECT_THAT(result.final_status(), Eq(Result::SIGNALED));
|
|
|
|
}
|
|
|
|
|
2023-03-02 17:25:02 +08:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
|
|
Instantiation, StackTraceTest,
|
|
|
|
::testing::Values(
|
|
|
|
TestCase{
|
2023-03-02 21:09:05 +08:00
|
|
|
.testname = "CrashMe",
|
|
|
|
.testno = 1,
|
2023-03-02 17:25:02 +08:00
|
|
|
.final_status = Result::SIGNALED,
|
|
|
|
.full_function_description = "CrashMe(char)",
|
|
|
|
},
|
|
|
|
TestCase{
|
2023-03-02 21:09:05 +08:00
|
|
|
.testname = "ViolatePolicy",
|
|
|
|
.testno = 2,
|
2023-03-02 17:25:02 +08:00
|
|
|
.final_status = Result::VIOLATION,
|
|
|
|
.full_function_description = "ViolatePolicy(int)",
|
|
|
|
},
|
|
|
|
TestCase{
|
2023-03-02 21:09:05 +08:00
|
|
|
.testname = "ExitNormally",
|
|
|
|
.testno = 3,
|
2023-03-02 17:25:02 +08:00
|
|
|
.final_status = Result::OK,
|
|
|
|
.full_function_description = "ExitNormally(int)",
|
|
|
|
.modify_policy =
|
|
|
|
[](PolicyBuilder* builder) {
|
|
|
|
builder->CollectStacktracesOnExit(true);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TestCase{
|
2023-03-02 21:09:05 +08:00
|
|
|
.testname = "SleepForXSeconds",
|
|
|
|
.testno = 4,
|
2023-03-02 17:25:02 +08:00
|
|
|
.final_status = Result::TIMEOUT,
|
|
|
|
.full_function_description = "SleepForXSeconds(int)",
|
|
|
|
.wall_time_limit = absl::Seconds(1),
|
2023-03-02 21:09:05 +08:00
|
|
|
},
|
|
|
|
TestCase{
|
|
|
|
.testname = "ViolatePolicyRecursive",
|
|
|
|
.testno = 2,
|
|
|
|
.testmode = 2,
|
|
|
|
.final_status = Result::VIOLATION,
|
|
|
|
.function_name = "ViolatePolicy",
|
|
|
|
.full_function_description = "ViolatePolicy(int)",
|
|
|
|
},
|
|
|
|
TestCase{
|
|
|
|
.testname = "ViolatePolicyRecursiveLib",
|
|
|
|
.testno = 2,
|
|
|
|
.testmode = 3,
|
|
|
|
.final_status = Result::VIOLATION,
|
|
|
|
.function_name = "ViolatePolicy",
|
|
|
|
.full_function_description = "ViolatePolicy(int)",
|
2024-02-27 20:35:44 +08:00
|
|
|
},
|
|
|
|
TestCase{
|
|
|
|
.testname = "ViolatePolicyForked",
|
|
|
|
.testno = 5,
|
|
|
|
.final_status = Result::VIOLATION,
|
|
|
|
.function_name = "ViolatePolicy",
|
|
|
|
.full_function_description = "ViolatePolicy(int)",
|
2023-03-02 17:25:02 +08:00
|
|
|
}),
|
|
|
|
[](const ::testing::TestParamInfo<TestCase>& info) {
|
2023-03-02 21:09:05 +08:00
|
|
|
return info.param.testname;
|
2023-03-02 17:25:02 +08:00
|
|
|
});
|
|
|
|
|
2019-07-09 16:31:48 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|