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/namespace.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-10-13 19:17:18 +08:00
|
|
|
#include <initializer_list>
|
|
|
|
#include <memory>
|
2023-03-06 16:27:06 +08:00
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <utility>
|
2023-03-06 16:27:06 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2023-03-01 21:35:51 +08:00
|
|
|
#include "sandboxed_api/sandbox2/allow_all_syscalls.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"
|
|
|
|
#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-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/temp_file.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
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::CreateNamedTempFile;
|
|
|
|
using ::sapi::GetTestSourcePath;
|
|
|
|
using ::sapi::GetTestTempPath;
|
2023-03-06 16:27:06 +08:00
|
|
|
using ::testing::Contains;
|
|
|
|
using ::testing::ElementsAre;
|
2021-10-13 19:17:18 +08:00
|
|
|
using ::testing::Eq;
|
2023-03-06 16:27:06 +08:00
|
|
|
using ::testing::Gt;
|
|
|
|
using ::testing::HasSubstr;
|
|
|
|
using ::testing::IsEmpty;
|
2021-10-13 19:17:18 +08:00
|
|
|
using ::testing::Ne;
|
2023-03-06 16:27:06 +08:00
|
|
|
using ::testing::SizeIs;
|
|
|
|
using ::testing::StartsWith;
|
|
|
|
using ::testing::StrEq; // sapi::google3-only(broken matchers)
|
2021-10-13 19:17:18 +08:00
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
std::string GetTestcaseBinPath(absl::string_view bin_name) {
|
|
|
|
return GetTestSourcePath(absl::StrCat("sandbox2/testcases/", bin_name));
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> RunSandboxeeWithArgsAndPolicy(
|
|
|
|
const std::string& bin_path, std::initializer_list<std::string> args,
|
|
|
|
std::unique_ptr<Policy> policy = nullptr) {
|
2023-03-03 22:50:17 +08:00
|
|
|
if (!policy) {
|
|
|
|
policy = CreateDefaultPermissiveTestPolicy(bin_path).BuildOrDie();
|
|
|
|
}
|
|
|
|
Sandbox2 sandbox(std::make_unique<Executor>(bin_path, args),
|
2021-10-13 19:17:18 +08:00
|
|
|
std::move(policy));
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
CHECK(sandbox.RunAsync());
|
|
|
|
Comms* comms = sandbox.comms();
|
|
|
|
uint64_t num;
|
|
|
|
|
|
|
|
std::vector<std::string> entries;
|
|
|
|
if (comms->RecvUint64(&num)) {
|
|
|
|
entries.reserve(num);
|
|
|
|
for (int i = 0; i < num; ++i) {
|
|
|
|
std::string entry;
|
|
|
|
CHECK(comms->RecvString(&entry));
|
|
|
|
entries.push_back(std::move(entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Result result = sandbox.AwaitResult();
|
2021-10-13 19:17:18 +08:00
|
|
|
EXPECT_THAT(result.final_status(), Eq(Result::OK));
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result.reason_code(), Eq(0));
|
|
|
|
return entries;
|
2021-10-13 19:17:18 +08:00
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(NamespaceTest, FileNamespaceWorks) {
|
2020-01-24 18:37:32 +08:00
|
|
|
// Mount /binary_path RO and check that it exists and is readable.
|
2019-03-19 00:21:48 +08:00
|
|
|
// /etc/passwd should not exist.
|
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.AddFileAt(path, "/binary_path")
|
|
|
|
.TryBuild());
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
2023-03-03 22:50:17 +08:00
|
|
|
path, {path, "0", "/binary_path", "/etc/passwd"}, std::move(policy));
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, ElementsAre("/binary_path"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-01-24 18:37:32 +08:00
|
|
|
TEST(NamespaceTest, ReadOnlyIsRespected) {
|
|
|
|
// Mount temporary file as RO and check that it actually is RO.
|
2020-04-02 22:42:17 +08:00
|
|
|
auto [name, fd] = CreateNamedTempFile(GetTestTempPath("temp_file")).value();
|
|
|
|
file_util::fileops::FDCloser temp_closer(fd);
|
2020-01-24 18:37:32 +08:00
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
2020-01-24 18:37:32 +08:00
|
|
|
{
|
2023-03-03 22:50:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.AddFileAt(name, "/temp_file")
|
|
|
|
.TryBuild());
|
2021-10-13 19:17:18 +08:00
|
|
|
// Check that it is readable
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
2023-03-03 22:50:17 +08:00
|
|
|
path, {path, "0", "/temp_file"}, std::move(policy));
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, ElementsAre("/temp_file"));
|
2020-01-24 18:37:32 +08:00
|
|
|
}
|
|
|
|
{
|
2023-03-03 22:50:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.AddFileAt(name, "/temp_file")
|
|
|
|
.TryBuild());
|
2021-10-13 19:17:18 +08:00
|
|
|
// Now check that it is not writeable
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
2023-03-03 22:50:17 +08:00
|
|
|
path, {path, "1", "/temp_file"}, std::move(policy));
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, IsEmpty());
|
2020-01-24 18:37:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(NamespaceTest, UserNamespaceWorks) {
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
2021-10-13 19:17:18 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Check that getpid() returns 2 (which is the case inside pid NS).
|
|
|
|
{
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result =
|
|
|
|
RunSandboxeeWithArgsAndPolicy(path, {path, "2"});
|
|
|
|
EXPECT_THAT(result, ElementsAre("2"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-10-13 19:17:18 +08:00
|
|
|
// Validate that getpid() does not return 2 when outside of a pid NS.
|
2019-03-19 00:21:48 +08:00
|
|
|
{
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
2021-10-13 19:17:18 +08:00
|
|
|
path, {path, "2"},
|
|
|
|
PolicyBuilder()
|
|
|
|
.DisableNamespaces()
|
2023-03-01 21:35:51 +08:00
|
|
|
.DefaultAction(AllowAllSyscalls()) // Do not restrict syscalls
|
2021-10-13 19:17:18 +08:00
|
|
|
.BuildOrDie());
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, ElementsAre(Ne("2")));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NamespaceTest, UserNamespaceIDMapWritten) {
|
|
|
|
// Check that the idmap is initialized before the sandbox application is
|
|
|
|
// started.
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
2019-03-19 00:21:48 +08:00
|
|
|
{
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result =
|
2023-03-03 22:50:17 +08:00
|
|
|
RunSandboxeeWithArgsAndPolicy(path, {path, "3", "1000", "1000"});
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, ElementsAre("1000", "1000"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the uid/gid is the same when not using namespaces.
|
|
|
|
{
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
|
|
|
path, {path, "3"},
|
2021-10-13 19:17:18 +08:00
|
|
|
PolicyBuilder()
|
|
|
|
.DisableNamespaces()
|
2023-03-01 21:35:51 +08:00
|
|
|
.DefaultAction(AllowAllSyscalls()) // Do not restrict syscalls
|
2021-10-13 19:17:18 +08:00
|
|
|
.BuildOrDie());
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result,
|
|
|
|
ElementsAre(absl::StrCat(getuid()), absl::StrCat(getgid())));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:51:01 +08:00
|
|
|
TEST(NamespaceTest, RootReadOnly) {
|
2021-10-13 19:17:18 +08:00
|
|
|
// Mount rw tmpfs at /tmp and check it is RW.
|
|
|
|
// Check also that / is RO.
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
auto policy, CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.AddTmpfs("/tmp", /*size=*/4ULL << 20 /* 4 MiB */)
|
|
|
|
.TryBuild());
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
2023-03-03 22:50:17 +08:00
|
|
|
path, {path, "4", "/tmp/testfile", "/testfile"}, std::move(policy));
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, ElementsAre("/tmp/testfile"));
|
2019-11-14 19:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NamespaceTest, RootWritable) {
|
|
|
|
// Mount root rw and check it
|
2023-03-03 22:50:17 +08:00
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
auto policy,
|
|
|
|
CreateDefaultPermissiveTestPolicy(path).SetRootWritable().TryBuild());
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
2023-03-03 22:50:17 +08:00
|
|
|
path, {path, "4", "/testfile"}, std::move(policy));
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, ElementsAre("/testfile"));
|
2019-11-14 19:51:01 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
TEST(NamespaceTest, HostnameNone) {
|
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
|
|
|
std::vector<std::string> result = RunSandboxeeWithArgsAndPolicy(
|
|
|
|
path, {path, "7"},
|
2021-10-13 19:17:18 +08:00
|
|
|
PolicyBuilder()
|
|
|
|
.DisableNamespaces()
|
2023-03-01 21:35:51 +08:00
|
|
|
.DefaultAction(AllowAllSyscalls()) // Do not restrict syscalls
|
2021-10-13 19:17:18 +08:00
|
|
|
.BuildOrDie());
|
2023-03-06 16:27:06 +08:00
|
|
|
EXPECT_THAT(result, ElementsAre(Ne("sandbox2")));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
TEST(NamespaceTest, HostnameDefault) {
|
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
|
|
|
std::vector<std::string> result =
|
|
|
|
RunSandboxeeWithArgsAndPolicy(path, {path, "7"});
|
|
|
|
EXPECT_THAT(result, ElementsAre("sandbox2"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
TEST(NamespaceTest, HostnameConfigured) {
|
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
2023-03-03 22:50:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.SetHostname("configured")
|
|
|
|
.TryBuild());
|
2023-03-06 16:27:06 +08:00
|
|
|
std::vector<std::string> result =
|
|
|
|
RunSandboxeeWithArgsAndPolicy(path, {path, "7"}, std::move(policy));
|
|
|
|
EXPECT_THAT(result, ElementsAre("configured"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NamespaceTest, TestInterfacesNoNetwork) {
|
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
|
|
|
std::vector<std::string> result =
|
|
|
|
RunSandboxeeWithArgsAndPolicy(path, {path, "5"});
|
|
|
|
// Only loopback network interface 'lo'.
|
|
|
|
EXPECT_THAT(result, ElementsAre("lo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NamespaceTest, TestInterfacesWithNetwork) {
|
|
|
|
const std::string path = GetTestcaseBinPath("namespace");
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy, CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.AllowUnrestrictedNetworking()
|
|
|
|
.TryBuild());
|
|
|
|
|
|
|
|
std::vector<std::string> result =
|
|
|
|
RunSandboxeeWithArgsAndPolicy(path, {path, "5"}, std::move(policy));
|
|
|
|
// Loopback network interface 'lo' and more.
|
|
|
|
EXPECT_THAT(result, Contains("lo"));
|
|
|
|
EXPECT_THAT(result, SizeIs(Gt(1)));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|