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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <syscall.h>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <unistd.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include <csignal>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2023-01-23 17:41:42 +08:00
|
|
|
#include <thread> // NOLINT(build/c++11)
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <utility>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
#include "absl/synchronization/notification.h"
|
|
|
|
#include "absl/time/clock.h"
|
|
|
|
#include "absl/time/time.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policybuilder.h"
|
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/testing.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2023-03-03 00:45:23 +08:00
|
|
|
using ::sapi::CreateDefaultPermissiveTestPolicy;
|
2021-01-14 01:25:25 +08:00
|
|
|
using ::sapi::GetTestSourcePath;
|
2023-03-09 00:08:35 +08:00
|
|
|
using ::sapi::IsOk;
|
2019-03-19 00:21:48 +08:00
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::IsEmpty;
|
2019-05-15 23:04:36 +08:00
|
|
|
using ::testing::IsTrue;
|
|
|
|
using ::testing::Lt;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
class Sandbox2Test : public ::testing::TestWithParam<bool> {
|
|
|
|
public:
|
|
|
|
PolicyBuilder CreateDefaultTestPolicy(absl::string_view path) {
|
|
|
|
PolicyBuilder builder = CreateDefaultPermissiveTestPolicy(path);
|
|
|
|
if (GetParam()) {
|
|
|
|
builder.CollectStacktracesOnSignal(false);
|
|
|
|
}
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
absl::Status SetUpSandbox(Sandbox2* sandbox) {
|
|
|
|
return GetParam() ? sandbox->EnableUnotifyMonitor() : absl::OkStatus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Test that aborting inside a sandbox with all userspace core dumping
|
|
|
|
// disabled reports the signal.
|
2023-03-09 00:08:35 +08:00
|
|
|
TEST_P(Sandbox2Test, AbortWithoutCoreDumpReturnsSignaled) {
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/abort");
|
|
|
|
std::vector<std::string> args = {
|
|
|
|
path,
|
|
|
|
};
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, CreateDefaultTestPolicy(path)
|
2023-02-16 23:11:48 +08:00
|
|
|
.TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
2023-03-09 00:08:35 +08:00
|
|
|
ASSERT_THAT(SetUpSandbox(&sandbox), IsOk());
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = sandbox.Run();
|
|
|
|
|
2019-03-26 22:54:02 +08:00
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::SIGNALED));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(SIGABRT));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
// Test that with TSYNC we are able to sandbox when multithreaded.
|
|
|
|
TEST_P(Sandbox2Test, TsyncNoMemoryChecks) {
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/tsync");
|
2022-02-16 21:58:43 +08:00
|
|
|
|
|
|
|
auto executor =
|
2022-10-12 20:22:51 +08:00
|
|
|
std::make_unique<Executor>(path, std::vector<std::string>{path});
|
2019-03-19 00:21:48 +08:00
|
|
|
executor->set_enable_sandbox_before_exec(false);
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-09 00:08:35 +08:00
|
|
|
CreateDefaultTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
2023-03-09 00:08:35 +08:00
|
|
|
ASSERT_THAT(SetUpSandbox(&sandbox), IsOk());
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = sandbox.Run();
|
|
|
|
|
|
|
|
// With TSYNC, SandboxMeHere should be able to sandbox when multithreaded.
|
|
|
|
ASSERT_EQ(result.final_status(), Result::OK);
|
|
|
|
ASSERT_EQ(result.reason_code(), 0);
|
|
|
|
}
|
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
// Tests whether Executor(fd, std::vector<std::string>{path}, envp) constructor
|
|
|
|
// works as expected.
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(ExecutorTest, ExecutorFdConstructor) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/minimal");
|
|
|
|
int fd = open(path.c_str(), O_RDONLY);
|
|
|
|
ASSERT_NE(fd, -1);
|
|
|
|
|
|
|
|
std::vector<std::string> args = {absl::StrCat("FD:", fd)};
|
2023-03-22 20:46:25 +08:00
|
|
|
auto executor = std::make_unique<Executor>(fd, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 00:45:23 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
|
|
|
auto result = sandbox.Run();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), Result::OK);
|
|
|
|
}
|
|
|
|
|
2023-02-16 23:11:48 +08:00
|
|
|
// Tests that we return the correct state when the sandboxee was killed by an
|
|
|
|
// external signal. Also make sure that we do not have the stack trace.
|
2023-03-09 00:08:35 +08:00
|
|
|
TEST_P(Sandbox2Test, SandboxeeExternalKill) {
|
2023-02-16 23:11:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/sleep");
|
|
|
|
|
|
|
|
std::vector<std::string> args = {path};
|
2023-03-22 20:46:25 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2023-02-16 23:11:48 +08:00
|
|
|
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-09 00:08:35 +08:00
|
|
|
CreateDefaultTestPolicy(path).TryBuild());
|
2023-02-16 23:11:48 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
2023-03-09 00:08:35 +08:00
|
|
|
ASSERT_THAT(SetUpSandbox(&sandbox), IsOk());
|
2023-02-16 23:11:48 +08:00
|
|
|
ASSERT_TRUE(sandbox.RunAsync());
|
2019-03-19 00:21:48 +08:00
|
|
|
sleep(1);
|
|
|
|
sandbox.Kill();
|
|
|
|
auto result = sandbox.AwaitResult();
|
|
|
|
EXPECT_EQ(result.final_status(), Result::EXTERNAL_KILL);
|
2023-03-02 17:25:02 +08:00
|
|
|
EXPECT_THAT(result.stack_trace(), IsEmpty());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-02-16 23:11:48 +08:00
|
|
|
// Tests that we do not collect stack traces if it was disabled (signaled).
|
2023-03-09 00:08:35 +08:00
|
|
|
TEST_P(Sandbox2Test, SandboxeeTimeoutDisabledStacktraces) {
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/sleep");
|
|
|
|
|
|
|
|
std::vector<std::string> args = {path};
|
2023-03-22 20:46:25 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, CreateDefaultTestPolicy(path)
|
2023-02-16 23:11:48 +08:00
|
|
|
.CollectStacktracesOnTimeout(false)
|
|
|
|
.TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
2023-03-09 00:08:35 +08:00
|
|
|
ASSERT_THAT(SetUpSandbox(&sandbox), IsOk());
|
2019-03-19 00:21:48 +08:00
|
|
|
ASSERT_TRUE(sandbox.RunAsync());
|
2019-11-29 00:06:37 +08:00
|
|
|
sandbox.set_walltime_limit(absl::Seconds(1));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = sandbox.AwaitResult();
|
|
|
|
EXPECT_EQ(result.final_status(), Result::TIMEOUT);
|
2023-03-02 17:25:02 +08:00
|
|
|
EXPECT_THAT(result.stack_trace(), IsEmpty());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-02-16 23:11:48 +08:00
|
|
|
// Tests that we do not collect stack traces if it was disabled (violation).
|
2023-03-09 00:08:35 +08:00
|
|
|
TEST(Sandbox2Test, SandboxeeViolationDisabledStacktraces) {
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/sleep");
|
|
|
|
|
|
|
|
std::vector<std::string> args = {path};
|
2023-03-22 20:46:25 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-02-16 23:11:48 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
auto policy, PolicyBuilder()
|
|
|
|
// Don't allow anything - Make sure that we'll crash.
|
|
|
|
.CollectStacktracesOnViolation(false)
|
2021-01-22 22:01:05 +08:00
|
|
|
.TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
|
|
|
ASSERT_TRUE(sandbox.RunAsync());
|
|
|
|
auto result = sandbox.AwaitResult();
|
|
|
|
EXPECT_EQ(result.final_status(), Result::VIOLATION);
|
2023-03-02 17:25:02 +08:00
|
|
|
EXPECT_THAT(result.stack_trace(), IsEmpty());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
TEST_P(Sandbox2Test, SandboxeeNotKilledWhenStartingThreadFinishes) {
|
2023-01-23 17:41:42 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/minimal");
|
|
|
|
std::vector<std::string> args = {path};
|
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
|
|
|
|
2023-03-03 00:45:23 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-09 00:08:35 +08:00
|
|
|
CreateDefaultTestPolicy(path).TryBuild());
|
2023-01-23 17:41:42 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
2023-03-09 00:08:35 +08:00
|
|
|
ASSERT_THAT(SetUpSandbox(&sandbox), IsOk());
|
2023-01-23 17:41:42 +08:00
|
|
|
std::thread sandbox_start_thread([&sandbox]() { sandbox.RunAsync(); });
|
|
|
|
sandbox_start_thread.join();
|
|
|
|
Result result = sandbox.AwaitResult();
|
|
|
|
EXPECT_EQ(result.final_status(), Result::OK);
|
|
|
|
}
|
|
|
|
|
2019-05-15 23:04:36 +08:00
|
|
|
TEST(StarvationTest, MonitorIsNotStarvedByTheSandboxee) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/starve");
|
|
|
|
|
|
|
|
std::vector<std::string> args = {path};
|
2023-03-22 20:46:25 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2022-02-16 21:58:43 +08:00
|
|
|
executor->limits()->set_walltime_limit(absl::Seconds(5));
|
2019-05-15 23:04:36 +08:00
|
|
|
|
2023-02-16 23:11:48 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 00:45:23 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-05-15 23:04:36 +08:00
|
|
|
Sandbox2 sandbox(std::move(executor), std::move(policy));
|
2022-02-16 21:58:43 +08:00
|
|
|
|
2019-05-15 23:04:36 +08:00
|
|
|
auto start = absl::Now();
|
|
|
|
ASSERT_THAT(sandbox.RunAsync(), IsTrue());
|
|
|
|
auto result = sandbox.AwaitResult();
|
|
|
|
EXPECT_THAT(result.final_status(), Eq(Result::TIMEOUT));
|
2022-02-16 21:58:43 +08:00
|
|
|
|
|
|
|
auto elapsed = absl::Now() - start;
|
2019-05-15 23:04:36 +08:00
|
|
|
EXPECT_THAT(elapsed, Lt(absl::Seconds(10)));
|
|
|
|
}
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
INSTANTIATE_TEST_SUITE_P(Sandbox2, Sandbox2Test, ::testing::Values(false, true),
|
|
|
|
[](const ::testing::TestParamInfo<bool>& info) {
|
|
|
|
return info.param ? "UnotifyMonitor"
|
|
|
|
: "PtraceMonitor";
|
|
|
|
});
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|