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/policy.h"
|
|
|
|
|
|
|
|
#include <syscall.h>
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <string>
|
2023-03-03 22:50:17 +08:00
|
|
|
#include <utility>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "absl/strings/string_view.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/policybuilder.h"
|
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
|
|
|
#include "sandboxed_api/sandbox2/util/bpf_helper.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/testing.h"
|
2023-03-03 22:50:17 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
using ::testing::Eq;
|
|
|
|
|
2020-09-10 20:47:32 +08:00
|
|
|
#ifdef SAPI_X86_64
|
2019-03-19 00:21:48 +08:00
|
|
|
// Test that 32-bit syscalls from 64-bit are disallowed.
|
|
|
|
TEST(PolicyTest, AMD64Syscall32PolicyAllowed) {
|
2022-06-08 21:51:02 +08:00
|
|
|
SKIP_ANDROID;
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
|
|
|
|
|
|
|
|
std::vector<std::string> args = {path, "1"};
|
2023-03-03 22:50:17 +08:00
|
|
|
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
2019-03-19 18:40:51 +08:00
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::VIOLATION));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(1)); // __NR_exit in 32-bit
|
|
|
|
EXPECT_THAT(result.GetSyscallArch(), Eq(sapi::cpu::kX86));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that 32-bit syscalls from 64-bit for FS checks are disallowed.
|
|
|
|
TEST(PolicyTest, AMD64Syscall32FsAllowed) {
|
2022-06-08 21:51:02 +08:00
|
|
|
SKIP_ANDROID;
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
|
|
|
|
std::vector<std::string> args = {path, "2"};
|
2023-03-03 22:50:17 +08:00
|
|
|
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
2019-03-19 18:40:51 +08:00
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::VIOLATION));
|
|
|
|
EXPECT_THAT(result.reason_code(),
|
|
|
|
Eq(33)); // __NR_access in 32-bit
|
|
|
|
EXPECT_THAT(result.GetSyscallArch(), Eq(sapi::cpu::kX86));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-09-10 20:47:32 +08:00
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Test that ptrace(2) is disallowed.
|
|
|
|
TEST(PolicyTest, PtraceDisallowed) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
|
|
|
|
std::vector<std::string> args = {path, "3"};
|
2023-03-03 22:50:17 +08:00
|
|
|
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::VIOLATION));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(__NR_ptrace));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that clone(2) with flag CLONE_UNTRACED is disallowed.
|
|
|
|
TEST(PolicyTest, CloneUntracedDisallowed) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
|
|
|
|
std::vector<std::string> args = {path, "4"};
|
2023-03-03 22:50:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::VIOLATION));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(__NR_clone));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that bpf(2) is disallowed.
|
|
|
|
TEST(PolicyTest, BpfDisallowed) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
|
|
|
|
std::vector<std::string> args = {path, "5"};
|
2023-03-03 22:50:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::VIOLATION));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(__NR_bpf));
|
|
|
|
}
|
|
|
|
|
2023-08-30 22:46:35 +08:00
|
|
|
// Test that ptrace/bpf can return EPERM.
|
|
|
|
TEST(PolicyTest, BpfPtracePermissionDenied) {
|
2020-12-03 00:37:55 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
|
|
|
|
std::vector<std::string> args = {path, "7"};
|
|
|
|
|
2023-08-30 22:46:35 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
auto policy, CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.BlockSyscallsWithErrno({__NR_ptrace, __NR_bpf}, EPERM)
|
|
|
|
.TryBuild());
|
2022-02-16 21:58:43 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2020-12-03 00:37:55 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
2023-08-30 22:46:35 +08:00
|
|
|
// ptrace/bpf is not a violation due to explicit policy. EPERM is expected.
|
2020-12-03 00:37:55 +08:00
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
2023-08-30 22:46:35 +08:00
|
|
|
EXPECT_THAT(result.reason_code(), Eq(0));
|
2020-12-03 00:37:55 +08:00
|
|
|
}
|
|
|
|
|
2019-03-20 19:10:57 +08:00
|
|
|
TEST(PolicyTest, IsattyAllowed) {
|
2023-08-31 15:43:30 +08:00
|
|
|
SKIP_SANITIZERS;
|
2023-08-31 21:05:17 +08:00
|
|
|
PolicyBuilder builder;
|
2023-03-03 22:50:17 +08:00
|
|
|
if constexpr (sapi::host_os::IsAndroid()) {
|
|
|
|
builder.DisableNamespaces().AllowDynamicStartup();
|
|
|
|
}
|
|
|
|
builder.AllowStaticStartup()
|
|
|
|
.AllowExit()
|
|
|
|
.AllowRead()
|
|
|
|
.AllowWrite()
|
2023-08-31 15:43:30 +08:00
|
|
|
.AllowTCGETS()
|
|
|
|
.AllowLlvmCoverage();
|
2019-03-20 19:10:57 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
|
|
|
|
std::vector<std::string> args = {path, "6"};
|
2023-03-03 22:50:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, builder.TryBuild());
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-20 19:10:57 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
|
|
|
}
|
|
|
|
|
2023-08-31 15:43:30 +08:00
|
|
|
std::unique_ptr<Policy> MinimalTestcasePolicy(absl::string_view path = "") {
|
2023-08-31 21:05:17 +08:00
|
|
|
PolicyBuilder builder;
|
2022-06-08 21:51:02 +08:00
|
|
|
|
|
|
|
if constexpr (sapi::host_os::IsAndroid()) {
|
|
|
|
builder.AllowDynamicStartup();
|
|
|
|
builder.DisableNamespaces();
|
|
|
|
}
|
|
|
|
|
2023-08-31 15:43:30 +08:00
|
|
|
builder.AllowStaticStartup().AllowExit().AllowLlvmCoverage();
|
2022-06-08 21:51:02 +08:00
|
|
|
return builder.BuildOrDie();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we can sandbox a minimal static binary returning 0.
|
|
|
|
// If this starts failing, it means something changed, maybe in the way we
|
|
|
|
// compile static binaries, and we need to update the policy just above.
|
|
|
|
TEST(MinimalTest, MinimalBinaryWorks) {
|
2022-06-08 21:51:02 +08:00
|
|
|
SKIP_ANDROID;
|
2023-08-31 15:43:30 +08:00
|
|
|
SKIP_SANITIZERS;
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/minimal");
|
|
|
|
std::vector<std::string> args = {path};
|
2023-08-31 15:43:30 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args),
|
|
|
|
MinimalTestcasePolicy(path));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(EXIT_SUCCESS));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we can sandbox a minimal non-static binary returning 0.
|
|
|
|
TEST(MinimalTest, MinimalSharedBinaryWorks) {
|
2023-08-31 15:43:30 +08:00
|
|
|
SKIP_SANITIZERS;
|
2019-03-26 22:54:02 +08:00
|
|
|
const std::string path =
|
|
|
|
GetTestSourcePath("sandbox2/testcases/minimal_dynamic");
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> args = {path};
|
|
|
|
|
2023-08-31 21:05:17 +08:00
|
|
|
PolicyBuilder builder;
|
2022-06-08 21:51:02 +08:00
|
|
|
|
|
|
|
if constexpr (sapi::host_os::IsAndroid()) {
|
|
|
|
builder.DisableNamespaces();
|
|
|
|
} else {
|
|
|
|
builder.AddLibrariesForBinary(path);
|
|
|
|
}
|
|
|
|
|
2023-08-31 15:43:30 +08:00
|
|
|
builder.AllowDynamicStartup().AllowExit().AllowLlvmCoverage();
|
2022-06-08 21:51:02 +08:00
|
|
|
auto policy = builder.BuildOrDie();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(EXIT_SUCCESS));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the AllowSystemMalloc helper works as expected.
|
|
|
|
TEST(MallocTest, SystemMallocWorks) {
|
2023-08-31 15:43:30 +08:00
|
|
|
SKIP_SANITIZERS;
|
2019-03-26 22:54:02 +08:00
|
|
|
const std::string path =
|
|
|
|
GetTestSourcePath("sandbox2/testcases/malloc_system");
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> args = {path};
|
|
|
|
|
2023-08-31 21:05:17 +08:00
|
|
|
PolicyBuilder builder;
|
2022-06-08 21:51:02 +08:00
|
|
|
|
|
|
|
if constexpr (sapi::host_os::IsAndroid()) {
|
|
|
|
builder.DisableNamespaces();
|
|
|
|
builder.AllowDynamicStartup();
|
|
|
|
builder.AllowSyscalls({
|
|
|
|
__NR_madvise,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-31 15:43:30 +08:00
|
|
|
builder.AllowStaticStartup()
|
|
|
|
.AllowSystemMalloc()
|
|
|
|
.AllowExit()
|
|
|
|
.AllowLlvmCoverage();
|
2022-06-08 21:51:02 +08:00
|
|
|
auto policy = builder.BuildOrDie();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(EXIT_SUCCESS));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complicated test to see that AddPolicyOnSyscalls work as
|
|
|
|
// expected. Specifically a worrisome corner-case would be that the logic was
|
|
|
|
// almost correct, but that the jump targets were off slightly. This uses the
|
|
|
|
// AddPolicyOnSyscall multiple times in a row to make any miscalculation
|
|
|
|
// unlikely to pass this check.
|
|
|
|
TEST(MultipleSyscalls, AddPolicyOnSyscallsWorks) {
|
|
|
|
SKIP_SANITIZERS_AND_COVERAGE;
|
|
|
|
const std::string path =
|
|
|
|
GetTestSourcePath("sandbox2/testcases/add_policy_on_syscalls");
|
|
|
|
std::vector<std::string> args = {path};
|
|
|
|
|
2023-08-31 21:05:17 +08:00
|
|
|
PolicyBuilder builder;
|
2022-06-08 21:51:02 +08:00
|
|
|
if constexpr (sapi::host_os::IsAndroid()) {
|
|
|
|
builder.DisableNamespaces();
|
|
|
|
builder.AllowDynamicStartup();
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.AllowStaticStartup()
|
|
|
|
.AllowTcMalloc()
|
|
|
|
.AllowExit()
|
|
|
|
.AddPolicyOnSyscalls(
|
|
|
|
{
|
|
|
|
__NR_getuid,
|
|
|
|
__NR_getgid,
|
|
|
|
__NR_geteuid,
|
|
|
|
__NR_getegid,
|
2020-12-17 01:17:53 +08:00
|
|
|
#ifdef __NR_getuid32
|
2022-06-08 21:51:02 +08:00
|
|
|
__NR_getuid32,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_getgid32
|
2022-06-08 21:51:02 +08:00
|
|
|
__NR_getgid32,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_geteuid32
|
2022-06-08 21:51:02 +08:00
|
|
|
__NR_geteuid32,
|
2020-09-11 21:33:57 +08:00
|
|
|
#endif
|
2020-12-17 01:17:53 +08:00
|
|
|
#ifdef __NR_getegid32
|
2022-06-08 21:51:02 +08:00
|
|
|
__NR_getegid32,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
2022-06-08 21:51:02 +08:00
|
|
|
},
|
|
|
|
{ALLOW})
|
|
|
|
.AddPolicyOnSyscalls(
|
|
|
|
{
|
|
|
|
__NR_getresuid,
|
|
|
|
__NR_getresgid,
|
2020-12-17 01:17:53 +08:00
|
|
|
#ifdef __NR_getresuid32
|
2022-06-08 21:51:02 +08:00
|
|
|
__NR_getresuid32,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_getresgid32
|
2022-06-08 21:51:02 +08:00
|
|
|
__NR_getresgid32,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
2022-06-08 21:51:02 +08:00
|
|
|
},
|
|
|
|
{ERRNO(42)})
|
|
|
|
.AddPolicyOnSyscalls({__NR_read, __NR_write}, {ERRNO(43)})
|
2023-08-29 23:11:31 +08:00
|
|
|
.AddPolicyOnSyscall(__NR_umask, {DENY});
|
2022-06-08 21:51:02 +08:00
|
|
|
auto policy = builder.BuildOrDie();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args), std::move(policy));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::VIOLATION));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(__NR_umask));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|