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/limits.h"
|
|
|
|
|
|
|
|
#include <csignal>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "sandboxed_api/sandbox2/executor.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"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
using ::sapi::CreateDefaultPermissiveTestPolicy;
|
2021-01-14 01:25:25 +08:00
|
|
|
using ::sapi::GetTestSourcePath;
|
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
std::string GetLimitsTestcaseBinPath() {
|
|
|
|
return GetTestSourcePath("sandbox2/testcases/limits");
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(LimitsTest, RLimitASMmapUnderLimit) {
|
2023-09-15 19:56:38 +08:00
|
|
|
SKIP_SANITIZERS;
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetLimitsTestcaseBinPath();
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> args = {path, "1"}; // mmap(1 MiB)
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<sandbox2::Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
sandbox2::Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), sandbox2::Result::OK);
|
|
|
|
EXPECT_EQ(result.reason_code(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LimitsTest, RLimitASMmapAboveLimit) {
|
2023-09-15 19:56:38 +08:00
|
|
|
SKIP_SANITIZERS;
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetLimitsTestcaseBinPath();
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> args = {path, "2"}; // mmap(100 MiB)
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<sandbox2::Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
sandbox2::Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), sandbox2::Result::OK);
|
|
|
|
EXPECT_EQ(result.reason_code(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LimitsTest, RLimitASAllocaSmallUnderLimit) {
|
2023-09-15 19:56:38 +08:00
|
|
|
SKIP_SANITIZERS;
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetLimitsTestcaseBinPath();
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> args = {path, "3"}; // alloca(1 MiB)
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<sandbox2::Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
sandbox2::Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), sandbox2::Result::OK);
|
|
|
|
EXPECT_EQ(result.reason_code(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LimitsTest, RLimitASAllocaBigUnderLimit) {
|
2023-09-15 19:56:38 +08:00
|
|
|
SKIP_SANITIZERS;
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetLimitsTestcaseBinPath();
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> args = {path, "4"}; // alloca(8 MiB)
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<sandbox2::Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
sandbox2::Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), sandbox2::Result::SIGNALED);
|
|
|
|
EXPECT_EQ(result.reason_code(), SIGSEGV);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LimitsTest, RLimitASAllocaBigAboveLimit) {
|
2023-09-15 19:56:38 +08:00
|
|
|
SKIP_SANITIZERS;
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetLimitsTestcaseBinPath();
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> args = {path, "5"}; // alloca(100 MiB)
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<sandbox2::Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
sandbox2::Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), sandbox2::Result::SIGNALED);
|
|
|
|
EXPECT_EQ(result.reason_code(), SIGSEGV);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|