mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
dbaf95c724
This change should make it less confusing where utility code comes from. Having it in two places made sense when we were debating whether to publish Sandbox2 separately, but not any longer. Follow-up changes will move `sandbox2/util.h` and rename the remaining `sandbox2/util` folder. PiperOrigin-RevId: 351601640 Change-Id: I6256845261f610e590c25e2c59851cc51da2d778
133 lines
5.4 KiB
C++
133 lines
5.4 KiB
C++
// Copyright 2019 Google LLC
|
|
//
|
|
// 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
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// 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 "gmock/gmock.h"
|
|
#include "gtest/gtest.h"
|
|
#include "absl/memory/memory.h"
|
|
#include "sandboxed_api/config.h"
|
|
#include "sandboxed_api/sandbox2/executor.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"
|
|
#include "sandboxed_api/sandbox2/util/bpf_helper.h"
|
|
#include "sandboxed_api/testing.h"
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
namespace sandbox2 {
|
|
namespace {
|
|
|
|
using ::sapi::GetTestSourcePath;
|
|
|
|
TEST(LimitsTest, RLimitASMmapUnderLimit) {
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/limits");
|
|
std::vector<std::string> args = {path, "1"}; // mmap(1 MiB)
|
|
auto executor = absl::make_unique<sandbox2::Executor>(path, args);
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, sandbox2::PolicyBuilder()
|
|
.DisableNamespaces()
|
|
// Don't restrict the syscalls at all.
|
|
.DangerDefaultAllowAll()
|
|
.TryBuild());
|
|
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) {
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/limits");
|
|
std::vector<std::string> args = {path, "2"}; // mmap(100 MiB)
|
|
auto executor = absl::make_unique<sandbox2::Executor>(path, args);
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, sandbox2::PolicyBuilder()
|
|
.DisableNamespaces()
|
|
// Don't restrict the syscalls at all.
|
|
.DangerDefaultAllowAll()
|
|
.TryBuild());
|
|
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) {
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/limits");
|
|
std::vector<std::string> args = {path, "3"}; // alloca(1 MiB)
|
|
auto executor = absl::make_unique<sandbox2::Executor>(path, args);
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, sandbox2::PolicyBuilder()
|
|
.DisableNamespaces()
|
|
// Don't restrict the syscalls at all.
|
|
.DangerDefaultAllowAll()
|
|
.TryBuild());
|
|
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) {
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/limits");
|
|
std::vector<std::string> args = {path, "4"}; // alloca(8 MiB)
|
|
auto executor = absl::make_unique<sandbox2::Executor>(path, args);
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, sandbox2::PolicyBuilder()
|
|
.DisableNamespaces()
|
|
// Don't restrict the syscalls at all.
|
|
.DangerDefaultAllowAll()
|
|
.TryBuild());
|
|
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) {
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/limits");
|
|
std::vector<std::string> args = {path, "5"}; // alloca(100 MiB)
|
|
auto executor = absl::make_unique<sandbox2::Executor>(path, args);
|
|
executor->limits()->set_rlimit_as(100ULL << 20); // 100 MiB
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, sandbox2::PolicyBuilder()
|
|
.DisableNamespaces()
|
|
// Don't restrict the syscalls at all.
|
|
.DangerDefaultAllowAll()
|
|
.TryBuild());
|
|
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
|