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
|
|
|
|
//
|
|
|
|
// 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/mounts.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2019-05-08 09:29:51 +08:00
|
|
|
#include "absl/strings/match.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/testing.h"
|
|
|
|
#include "sandboxed_api/util/file_helpers.h"
|
|
|
|
#include "sandboxed_api/util/path.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 = ::sapi::file;
|
|
|
|
using ::sapi::CreateNamedTempFileAndClose;
|
|
|
|
using ::sapi::CreateTempDir;
|
|
|
|
using ::sapi::GetTestSourcePath;
|
|
|
|
using ::sapi::GetTestTempPath;
|
|
|
|
using ::sapi::IsOk;
|
|
|
|
using ::sapi::StatusIs;
|
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::UnorderedElementsAreArray;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
constexpr size_t kTmpfsSize = 1024;
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestInvalidFilenames) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(mounts.AddFile(""), StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFile("a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", ""),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("", "/a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", "a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFile("/"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", "/"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestAddFile) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/b"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/e"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/dd/e"), IsOk());
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", "/f"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestAddDir) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/a", "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/c/d", "/c/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/c/d/e", "/c/d/e"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestAddTmpFs) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddTmpfs("/a", kTmpfsSize), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddTmpfs("/a/b", kTmpfsSize), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/a/b/c"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/a/b/d", "/a/b/d"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestMultipleInsertionFileSymlink) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
2020-04-02 22:42:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string path,
|
2021-01-22 22:01:05 +08:00
|
|
|
CreateNamedTempFileAndClose(
|
|
|
|
file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string symlink_path,
|
2021-01-22 22:01:05 +08:00
|
|
|
CreateNamedTempFileAndClose(
|
|
|
|
file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
ASSERT_THAT(unlink(symlink_path.c_str()), Eq(0));
|
|
|
|
ASSERT_THAT(symlink(path.c_str(), symlink_path.c_str()), Eq(0));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFileAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFileAt(symlink_path, "/a"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestMultipleInsertionDirSymlink) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
std::string path,
|
|
|
|
CreateTempDir(file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string symlink_path,
|
2021-01-22 22:01:05 +08:00
|
|
|
CreateNamedTempFileAndClose(
|
|
|
|
file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
ASSERT_THAT(unlink(symlink_path.c_str()), Eq(0));
|
|
|
|
ASSERT_THAT(symlink(path.c_str(), symlink_path.c_str()), Eq(0));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(symlink_path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(symlink_path, "/a"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestMultipleInsertion) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/d"), IsOk());
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/f", "/c"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/f", "/c"), IsOk());
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/d/e"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/f", "/c/d/e"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/f", "/c/d/e"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestEvilNullByte) {
|
|
|
|
Mounts mounts;
|
|
|
|
// create the filename with a null byte this way as g4 fix forces newlines
|
|
|
|
// otherwise.
|
2020-04-02 22:42:17 +08:00
|
|
|
std::string filename = "/a/b";
|
2019-03-19 00:21:48 +08:00
|
|
|
filename[2] = '\0';
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile(filename),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt(filename, "/a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", filename),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(filename, "/a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/a", filename),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddTmpfs(filename, kTmpfsSize),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestMinimalDynamicBinary) {
|
|
|
|
Mounts mounts;
|
|
|
|
EXPECT_THAT(mounts.AddMappingsForBinary(
|
|
|
|
GetTestSourcePath("sandbox2/testcases/minimal_dynamic")),
|
|
|
|
IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/lib/x86_64-linux-gnu/libc.so.6"), IsOk());
|
|
|
|
}
|
|
|
|
|
2019-05-08 09:29:51 +08:00
|
|
|
TEST(MountTreeTest, TestList) {
|
|
|
|
struct TestCase {
|
|
|
|
const char *path;
|
|
|
|
const bool is_ro;
|
|
|
|
};
|
|
|
|
// clang-format off
|
|
|
|
const TestCase test_cases[] = {
|
|
|
|
// NOTE: Directories have a trailing '/'; files don't.
|
|
|
|
{"/a/b", true},
|
|
|
|
{"/a/c/", true},
|
|
|
|
{"/a/c/d/e/f/g", true},
|
|
|
|
{"/h", true},
|
|
|
|
{"/i/j/k", false},
|
|
|
|
{"/i/l/", false},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
// Create actual directories and files on disk and selectively add
|
|
|
|
for (const auto &test_case : test_cases) {
|
|
|
|
const auto inside_path = test_case.path;
|
|
|
|
const std::string outside_path = absl::StrCat("/some/dir/", inside_path);
|
|
|
|
if (absl::EndsWith(outside_path, "/")) {
|
|
|
|
ASSERT_THAT(
|
|
|
|
mounts.AddDirectoryAt(file::CleanPath(outside_path),
|
|
|
|
file::CleanPath(inside_path), test_case.is_ro),
|
|
|
|
IsOk());
|
|
|
|
} else {
|
|
|
|
ASSERT_THAT(
|
|
|
|
mounts.AddFileAt(file::CleanPath(outside_path),
|
|
|
|
file::CleanPath(inside_path), test_case.is_ro),
|
|
|
|
IsOk());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 17:37:28 +08:00
|
|
|
ASSERT_THAT(mounts.AddTmpfs(file::CleanPath("/d"), 1024 * 1024), IsOk());
|
|
|
|
|
2019-05-08 09:29:51 +08:00
|
|
|
std::vector<std::string> outside_entries;
|
|
|
|
std::vector<std::string> inside_entries;
|
|
|
|
mounts.RecursivelyListMounts(&outside_entries, &inside_entries);
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
EXPECT_THAT(
|
|
|
|
inside_entries,
|
|
|
|
UnorderedElementsAreArray({
|
|
|
|
"R /a/b",
|
|
|
|
"R /a/c/",
|
|
|
|
"R /a/c/d/e/f/g",
|
|
|
|
"R /h",
|
|
|
|
"W /i/j/k",
|
|
|
|
"W /i/l/",
|
2019-10-24 17:37:28 +08:00
|
|
|
"/d",
|
2019-05-08 09:29:51 +08:00
|
|
|
}));
|
|
|
|
EXPECT_THAT(
|
|
|
|
outside_entries,
|
|
|
|
UnorderedElementsAreArray({
|
|
|
|
absl::StrCat("/some/dir/", "a/b"),
|
|
|
|
absl::StrCat("/some/dir/", "a/c/"),
|
|
|
|
absl::StrCat("/some/dir/", "a/c/d/e/f/g"),
|
|
|
|
absl::StrCat("/some/dir/", "h"),
|
|
|
|
absl::StrCat("/some/dir/", "i/j/k"),
|
|
|
|
absl::StrCat("/some/dir/", "i/l/"),
|
2019-10-24 17:37:28 +08:00
|
|
|
absl::StrCat("tmpfs: size=", 1024*1024),
|
2019-05-08 09:29:51 +08:00
|
|
|
}));
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|