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/mounts.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2023-08-03 22:39:37 +08:00
|
|
|
#include <string>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <tuple>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <utility>
|
2023-08-03 22:39:37 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include "absl/container/flat_hash_set.h"
|
2021-05-10 22:03:15 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/strings/match.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2020-01-27 19:19:28 +08:00
|
|
|
#include "absl/strings/str_join.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_split.h"
|
|
|
|
#include "absl/strings/string_view.h"
|
2021-02-19 16:43:06 +08:00
|
|
|
#include "absl/strings/strip.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2022-03-14 20:14:41 +08:00
|
|
|
#include "sandboxed_api/sandbox2/mount_tree.pb.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util/minielf.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
|
|
|
#include "sandboxed_api/util/path.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace cpu = ::sapi::cpu;
|
|
|
|
namespace file_util = ::sapi::file_util;
|
|
|
|
namespace host_cpu = ::sapi::host_cpu;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
bool PathContainsNullByte(absl::string_view path) {
|
2021-04-08 17:50:54 +08:00
|
|
|
return absl::StrContains(path, '\0');
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
absl::string_view GetOutsidePath(const MountTree::Node& node) {
|
|
|
|
switch (node.node_case()) {
|
|
|
|
case MountTree::Node::kFileNode:
|
|
|
|
return node.file_node().outside();
|
|
|
|
case MountTree::Node::kDirNode:
|
|
|
|
return node.dir_node().outside();
|
|
|
|
default:
|
|
|
|
SAPI_RAW_LOG(FATAL, "Invalid node type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> ExistingPathInsideDir(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::string_view dir_path, absl::string_view relative_path) {
|
2021-02-19 16:43:06 +08:00
|
|
|
auto path =
|
|
|
|
sapi::file::CleanPath(sapi::file::JoinPath(dir_path, relative_path));
|
2019-03-19 00:21:48 +08:00
|
|
|
if (file_util::fileops::StripBasename(path) != dir_path) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError("Relative path goes above the base dir");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (!file_util::fileops::Exists(path, false)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::NotFoundError(absl::StrCat("Does not exist: ", path));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ValidateInterpreter(absl::string_view interpreter) {
|
2019-03-19 00:21:48 +08:00
|
|
|
const absl::flat_hash_set<std::string> allowed_interpreters = {
|
|
|
|
"/lib64/ld-linux-x86-64.so.2",
|
2020-09-11 21:33:57 +08:00
|
|
|
"/lib64/ld64.so.2", // PPC64
|
|
|
|
"/lib/ld-linux-aarch64.so.1", // AArch64
|
2020-12-17 01:17:53 +08:00
|
|
|
"/lib/ld-linux-armhf.so.3", // Arm
|
2022-03-31 00:45:31 +08:00
|
|
|
"/system/bin/linker64", // android_arm64
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!allowed_interpreters.contains(interpreter)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Interpreter not on the whitelist: ", interpreter));
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ResolveLibraryPath(absl::string_view lib_name,
|
|
|
|
const std::vector<std::string>& search_paths) {
|
|
|
|
for (const auto& search_path : search_paths) {
|
2020-04-02 22:42:17 +08:00
|
|
|
if (auto path_or = ExistingPathInsideDir(search_path, lib_name);
|
|
|
|
path_or.ok()) {
|
|
|
|
return path_or.value();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:47:32 +08:00
|
|
|
constexpr absl::string_view GetPlatformCPUName() {
|
|
|
|
switch (host_cpu::Architecture()) {
|
|
|
|
case cpu::kX8664:
|
|
|
|
return "x86_64";
|
|
|
|
case cpu::kPPC64LE:
|
|
|
|
return "ppc64";
|
2020-09-11 21:33:57 +08:00
|
|
|
case cpu::kArm64:
|
|
|
|
return "aarch64";
|
2020-09-10 20:47:32 +08:00
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
std::string GetPlatform(absl::string_view interpreter) {
|
2020-09-10 20:47:32 +08:00
|
|
|
return absl::StrCat(GetPlatformCPUName(), "-linux-gnu");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-03-08 20:04:15 +08:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
bool IsSameFile(const std::string& path1, const std::string& path2) {
|
2022-03-09 17:55:58 +08:00
|
|
|
if (path1 == path2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-08 20:04:15 +08:00
|
|
|
struct stat stat1, stat2;
|
|
|
|
if (stat(path1.c_str(), &stat1) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(path2.c_str(), &stat2) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino;
|
|
|
|
}
|
|
|
|
|
2023-07-18 17:44:18 +08:00
|
|
|
bool IsWritable(const MountTree::Node& node) {
|
|
|
|
switch (node.node_case()) {
|
|
|
|
case MountTree::Node::kFileNode:
|
|
|
|
return node.file_node().writable();
|
|
|
|
case MountTree::Node::kDirNode:
|
|
|
|
return node.dir_node().writable();
|
|
|
|
case MountTree::Node::kRootNode:
|
|
|
|
return node.root_node().writable();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasSameTarget(const MountTree::Node& n1, const MountTree::Node& n2) {
|
2022-03-09 17:55:58 +08:00
|
|
|
// Return early when node types are different
|
2022-03-08 20:04:15 +08:00
|
|
|
if (n1.node_case() != n2.node_case()) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-09 17:55:58 +08:00
|
|
|
// Compare proto fileds
|
2022-03-08 20:04:15 +08:00
|
|
|
switch (n1.node_case()) {
|
2022-03-09 17:55:58 +08:00
|
|
|
case MountTree::Node::kFileNode:
|
|
|
|
// Check whether files are the same (e.g. symlinks / hardlinks)
|
2023-07-18 17:44:18 +08:00
|
|
|
return IsSameFile(n1.file_node().outside(), n2.file_node().outside());
|
2022-03-09 17:55:58 +08:00
|
|
|
case MountTree::Node::kDirNode:
|
|
|
|
// Check whether dirs are the same (e.g. symlinks / hardlinks)
|
2023-07-18 17:44:18 +08:00
|
|
|
return IsSameFile(n1.dir_node().outside(), n2.dir_node().outside());
|
2022-03-09 17:55:58 +08:00
|
|
|
case MountTree::Node::kTmpfsNode:
|
|
|
|
return n1.tmpfs_node().tmpfs_options() == n2.tmpfs_node().tmpfs_options();
|
2023-07-18 17:44:18 +08:00
|
|
|
case MountTree::Node::kRootNode:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEquivalentNode(const MountTree::Node& n1, const MountTree::Node& n2) {
|
|
|
|
if (!HasSameTarget(n1, n2)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare proto fileds
|
|
|
|
switch (n1.node_case()) {
|
|
|
|
case MountTree::Node::kFileNode:
|
|
|
|
return n1.file_node().writable() == n2.file_node().writable();
|
|
|
|
case MountTree::Node::kDirNode:
|
|
|
|
return n1.dir_node().writable() == n2.dir_node().writable();
|
|
|
|
case MountTree::Node::kTmpfsNode:
|
|
|
|
return true;
|
2022-03-09 17:55:58 +08:00
|
|
|
case MountTree::Node::kRootNode:
|
2022-03-14 20:14:41 +08:00
|
|
|
return n1.root_node().writable() == n2.root_node().writable();
|
2022-03-08 20:04:15 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2023-01-19 21:44:11 +08:00
|
|
|
absl::Status Mounts::Remove(absl::string_view path) {
|
|
|
|
if (PathContainsNullByte(path)) {
|
|
|
|
return absl::InvalidArgumentError(
|
|
|
|
absl::StrCat("Path contains a null byte: ", path));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string fixed_path = sapi::file::CleanPath(path);
|
|
|
|
if (!sapi::file::IsAbsolutePath(fixed_path)) {
|
|
|
|
return absl::InvalidArgumentError("Only absolute paths are supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixed_path == "/") {
|
|
|
|
return absl::InvalidArgumentError("Cannot remove root");
|
|
|
|
}
|
|
|
|
std::vector<absl::string_view> parts =
|
|
|
|
absl::StrSplit(absl::StripPrefix(fixed_path, "/"), '/');
|
|
|
|
|
|
|
|
MountTree* curtree = &mount_tree_;
|
|
|
|
for (absl::string_view part : parts) {
|
|
|
|
if (curtree->has_node() && curtree->node().has_file_node()) {
|
|
|
|
return absl::NotFoundError(
|
|
|
|
absl::StrCat("File node is mounted at parent of: ", path));
|
|
|
|
}
|
2023-01-19 23:31:23 +08:00
|
|
|
auto it = curtree->mutable_entries()->find(std::string(part));
|
2023-01-19 21:44:11 +08:00
|
|
|
if (it == curtree->mutable_entries()->end()) {
|
|
|
|
return absl::NotFoundError(
|
|
|
|
absl::StrCat("Path does not exist in mounts: ", path));
|
|
|
|
}
|
|
|
|
curtree = &it->second;
|
|
|
|
}
|
|
|
|
curtree->clear_node();
|
|
|
|
curtree->clear_entries();
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Mounts::Insert(absl::string_view path,
|
2019-08-23 23:08:23 +08:00
|
|
|
const MountTree::Node& new_node) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// Some sandboxes allow the inside/outside paths to be partially
|
|
|
|
// user-controlled with some sanitization.
|
|
|
|
// Since we're handling C++ strings and later convert them to C style
|
|
|
|
// strings, a null byte in a path component might silently truncate the path
|
|
|
|
// and mount something not expected by the caller. Check for null bytes in the
|
|
|
|
// strings to protect against this.
|
|
|
|
if (PathContainsNullByte(path)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Inside path contains a null byte: ", path));
|
|
|
|
}
|
|
|
|
switch (new_node.node_case()) {
|
|
|
|
case MountTree::Node::kFileNode:
|
|
|
|
case MountTree::Node::kDirNode: {
|
|
|
|
auto outside_path = GetOutsidePath(new_node);
|
|
|
|
if (outside_path.empty()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError("Outside path cannot be empty");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (PathContainsNullByte(outside_path)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Outside path contains a null byte: ", outside_path));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-11-14 19:51:01 +08:00
|
|
|
case MountTree::Node::kRootNode:
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError("Cannot insert a RootNode");
|
2019-03-19 00:21:48 +08:00
|
|
|
case MountTree::Node::kTmpfsNode:
|
|
|
|
case MountTree::Node::NODE_NOT_SET:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:43:06 +08:00
|
|
|
std::string fixed_path = sapi::file::CleanPath(path);
|
|
|
|
if (!sapi::file::IsAbsolutePath(fixed_path)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError("Only absolute paths are supported");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fixed_path == "/") {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError("The root already exists");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-02-19 16:43:06 +08:00
|
|
|
std::vector<absl::string_view> parts =
|
|
|
|
absl::StrSplit(absl::StripPrefix(fixed_path, "/"), '/');
|
|
|
|
std::string final_part(parts.back());
|
|
|
|
parts.pop_back();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
MountTree* curtree = &mount_tree_;
|
2021-02-19 16:43:06 +08:00
|
|
|
for (absl::string_view part : parts) {
|
2019-03-19 00:21:48 +08:00
|
|
|
curtree = &(curtree->mutable_entries()
|
2021-02-19 16:43:06 +08:00
|
|
|
->insert({std::string(part), MountTree()})
|
2019-03-19 00:21:48 +08:00
|
|
|
.first->second);
|
|
|
|
if (curtree->has_node() && curtree->node().has_file_node()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Cannot insert ", path,
|
|
|
|
" since a file is mounted as a parent directory"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curtree = &(curtree->mutable_entries()
|
|
|
|
->insert({final_part, MountTree()})
|
|
|
|
.first->second);
|
|
|
|
|
|
|
|
if (curtree->has_node()) {
|
2022-03-08 20:04:15 +08:00
|
|
|
if (internal::IsEquivalentNode(curtree->node(), new_node)) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(INFO, "Inserting %s with the same value twice",
|
|
|
|
std::string(path).c_str());
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2023-07-18 17:44:18 +08:00
|
|
|
if (internal::HasSameTarget(curtree->node(), new_node)) {
|
|
|
|
if (!internal::IsWritable(curtree->node()) &&
|
|
|
|
internal::IsWritable(new_node)) {
|
|
|
|
SAPI_RAW_LOG(INFO,
|
|
|
|
"Chaning %s to writable, was insterted read-only before",
|
|
|
|
std::string(path).c_str());
|
|
|
|
*curtree->mutable_node() = new_node;
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
if (internal::IsWritable(curtree->node()) &&
|
|
|
|
!internal::IsWritable(new_node)) {
|
|
|
|
SAPI_RAW_LOG(INFO,
|
|
|
|
"Inserting %s read-only is a nop, as it was insterted "
|
|
|
|
"writable before",
|
|
|
|
std::string(path).c_str());
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(absl::StrCat(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Inserting ", path, " twice with conflicting values ",
|
|
|
|
curtree->node().DebugString(), " vs. ", new_node.DebugString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_node.has_file_node() && !curtree->entries().empty()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Trying to mount file over existing directory at ", path));
|
|
|
|
}
|
|
|
|
|
|
|
|
*curtree->mutable_node() = new_node;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Mounts::AddFileAt(absl::string_view outside,
|
2019-08-23 23:08:23 +08:00
|
|
|
absl::string_view inside, bool is_ro) {
|
2019-03-19 00:21:48 +08:00
|
|
|
MountTree::Node node;
|
|
|
|
auto* file_node = node.mutable_file_node();
|
|
|
|
file_node->set_outside(std::string(outside));
|
2022-03-14 20:14:41 +08:00
|
|
|
file_node->set_writable(!is_ro);
|
2019-03-19 00:21:48 +08:00
|
|
|
return Insert(inside, node);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Mounts::AddDirectoryAt(absl::string_view outside,
|
2019-08-23 23:08:23 +08:00
|
|
|
absl::string_view inside, bool is_ro) {
|
2019-03-19 00:21:48 +08:00
|
|
|
MountTree::Node node;
|
|
|
|
auto dir_node = node.mutable_dir_node();
|
|
|
|
dir_node->set_outside(std::string(outside));
|
2022-03-14 20:14:41 +08:00
|
|
|
dir_node->set_writable(!is_ro);
|
2019-03-19 00:21:48 +08:00
|
|
|
return Insert(inside, node);
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:43:06 +08:00
|
|
|
absl::StatusOr<std::string> Mounts::ResolvePath(absl::string_view path) const {
|
|
|
|
if (!sapi::file::IsAbsolutePath(path)) {
|
|
|
|
return absl::InvalidArgumentError("Path has to be absolute");
|
2019-10-23 00:05:13 +08:00
|
|
|
}
|
2021-02-19 16:43:06 +08:00
|
|
|
std::string fixed_path = sapi::file::CleanPath(path);
|
|
|
|
absl::string_view tail = absl::StripPrefix(fixed_path, "/");
|
2019-10-23 00:05:13 +08:00
|
|
|
|
|
|
|
const MountTree* curtree = &mount_tree_;
|
2021-02-19 16:43:06 +08:00
|
|
|
while (!tail.empty()) {
|
|
|
|
std::pair<absl::string_view, absl::string_view> parts =
|
|
|
|
absl::StrSplit(tail, absl::MaxSplits('/', 1));
|
2021-04-08 17:50:54 +08:00
|
|
|
const std::string cur(parts.first);
|
2021-02-19 16:43:06 +08:00
|
|
|
const auto it = curtree->entries().find(cur);
|
|
|
|
if (it == curtree->entries().end()) {
|
|
|
|
if (curtree->node().has_dir_node()) {
|
|
|
|
return sapi::file::JoinPath(curtree->node().dir_node().outside(), tail);
|
|
|
|
}
|
|
|
|
return absl::NotFoundError("Path could not be resolved in the mounts");
|
2019-10-23 00:05:13 +08:00
|
|
|
}
|
2021-02-19 16:43:06 +08:00
|
|
|
curtree = &it->second;
|
2021-04-14 17:45:15 +08:00
|
|
|
tail = parts.second;
|
2021-02-19 16:43:06 +08:00
|
|
|
}
|
|
|
|
switch (curtree->node().node_case()) {
|
|
|
|
case MountTree::Node::kFileNode:
|
|
|
|
case MountTree::Node::kDirNode:
|
|
|
|
return std::string(GetOutsidePath(curtree->node()));
|
|
|
|
case MountTree::Node::kRootNode:
|
|
|
|
case MountTree::Node::kTmpfsNode:
|
|
|
|
case MountTree::Node::NODE_NOT_SET:
|
|
|
|
break;
|
2019-10-23 00:05:13 +08:00
|
|
|
}
|
2021-02-19 16:43:06 +08:00
|
|
|
return absl::NotFoundError("Path could not be resolved in the mounts");
|
2019-10-23 00:05:13 +08:00
|
|
|
}
|
|
|
|
|
2019-05-11 00:40:42 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void LogContainer(const std::vector<std::string>& container) {
|
|
|
|
for (size_t i = 0; i < container.size(); ++i) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(INFO, "[%4zd]=%s", i, container[i].c_str());
|
2019-05-11 00:40:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Mounts::AddMappingsForBinary(const std::string& path,
|
2019-08-23 23:08:23 +08:00
|
|
|
absl::string_view ld_library_path) {
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
auto elf,
|
|
|
|
ElfFile::ParseFromFile(
|
|
|
|
path, ElfFile::kGetInterpreter | ElfFile::kLoadImportedLibraries));
|
2020-04-02 22:42:17 +08:00
|
|
|
const std::string& interpreter = elf.interpreter();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
if (interpreter.empty()) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "The file %s is not a dynamic executable", path.c_str());
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "The file %s is using interpreter %s", path.c_str(),
|
|
|
|
interpreter.c_str());
|
2019-03-29 22:07:03 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(ValidateInterpreter(interpreter));
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
std::vector<std::string> search_paths;
|
2021-05-17 19:06:39 +08:00
|
|
|
// 1. LD_LIBRARY_PATH
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!ld_library_path.empty()) {
|
|
|
|
std::vector<std::string> ld_library_paths =
|
|
|
|
absl::StrSplit(ld_library_path, absl::ByAnyChar(":;"));
|
|
|
|
search_paths.insert(search_paths.end(), ld_library_paths.begin(),
|
|
|
|
ld_library_paths.end());
|
|
|
|
}
|
|
|
|
// 2. Standard paths
|
|
|
|
search_paths.insert(search_paths.end(), {
|
|
|
|
"/lib",
|
|
|
|
"/lib64",
|
|
|
|
"/usr/lib",
|
|
|
|
"/usr/lib64",
|
|
|
|
});
|
|
|
|
std::vector<std::string> hw_cap_paths = {
|
|
|
|
GetPlatform(interpreter),
|
|
|
|
"tls",
|
|
|
|
};
|
|
|
|
std::vector<std::string> full_search_paths;
|
|
|
|
for (const auto& search_path : search_paths) {
|
|
|
|
for (int hw_caps_set = (1 << hw_cap_paths.size()) - 1; hw_caps_set >= 0;
|
|
|
|
--hw_caps_set) {
|
|
|
|
std::string path = search_path;
|
|
|
|
for (int hw_cap = 0; hw_cap < hw_cap_paths.size(); ++hw_cap) {
|
|
|
|
if ((hw_caps_set & (1 << hw_cap)) != 0) {
|
2021-02-19 16:43:06 +08:00
|
|
|
path = sapi::file::JoinPath(path, hw_cap_paths[hw_cap]);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file_util::fileops::Exists(path, /*fully_resolve=*/false)) {
|
|
|
|
full_search_paths.push_back(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arbitrary cut-off values, so we can safely resolve the libs.
|
|
|
|
constexpr int kMaxWorkQueueSize = 1000;
|
|
|
|
constexpr int kMaxResolvingDepth = 10;
|
|
|
|
constexpr int kMaxResolvedEntries = 1000;
|
|
|
|
constexpr int kMaxLoadedEntries = 100;
|
|
|
|
constexpr int kMaxImportedLibraries = 100;
|
|
|
|
|
|
|
|
absl::flat_hash_set<std::string> imported_libraries;
|
|
|
|
std::vector<std::pair<std::string, int>> to_resolve;
|
|
|
|
{
|
|
|
|
auto imported_libs = elf.imported_libraries();
|
|
|
|
if (imported_libs.size() > kMaxWorkQueueSize) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Exceeded max entries pending resolving limit");
|
|
|
|
}
|
|
|
|
for (const auto& imported_lib : imported_libs) {
|
|
|
|
to_resolve.emplace_back(imported_lib, 1);
|
|
|
|
}
|
2019-05-11 00:40:42 +08:00
|
|
|
|
2023-08-18 18:11:57 +08:00
|
|
|
if (SAPI_RAW_VLOG_IS_ON(1)) {
|
2019-05-11 00:40:42 +08:00
|
|
|
SAPI_RAW_VLOG(
|
|
|
|
1, "Resolving dynamic library dependencies of %s using these dirs:",
|
2020-12-14 19:33:29 +08:00
|
|
|
path.c_str());
|
2019-05-11 00:40:42 +08:00
|
|
|
LogContainer(full_search_paths);
|
|
|
|
}
|
2023-08-18 18:11:57 +08:00
|
|
|
if (SAPI_RAW_VLOG_IS_ON(2)) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(2, "Direct dependencies of %s to resolve:", path.c_str());
|
2019-05-11 00:40:42 +08:00
|
|
|
LogContainer(imported_libs);
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2019-05-11 00:40:42 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// This is DFS with an auxiliary stack
|
|
|
|
int resolved = 0;
|
|
|
|
int loaded = 0;
|
|
|
|
while (!to_resolve.empty()) {
|
|
|
|
int depth;
|
|
|
|
std::string lib;
|
|
|
|
std::tie(lib, depth) = to_resolve.back();
|
|
|
|
to_resolve.pop_back();
|
|
|
|
++resolved;
|
|
|
|
if (resolved > kMaxResolvedEntries) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Exceeded max resolved entries limit");
|
|
|
|
}
|
|
|
|
if (depth > kMaxResolvingDepth) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Exceeded max resolving depth limit");
|
|
|
|
}
|
|
|
|
std::string resolved_lib = ResolveLibraryPath(lib, full_search_paths);
|
|
|
|
if (resolved_lib.empty()) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Failed to resolve library: %s", lib.c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (imported_libraries.contains(resolved_lib)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-11 00:40:42 +08:00
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "Resolved library: %s => %s", lib.c_str(),
|
|
|
|
resolved_lib.c_str());
|
2019-05-11 00:40:42 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
imported_libraries.insert(resolved_lib);
|
|
|
|
if (imported_libraries.size() > kMaxImportedLibraries) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Exceeded max imported libraries limit");
|
|
|
|
}
|
|
|
|
++loaded;
|
|
|
|
if (loaded > kMaxLoadedEntries) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("Exceeded max loaded entries limit");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
auto lib_elf,
|
|
|
|
ElfFile::ParseFromFile(resolved_lib, ElfFile::kLoadImportedLibraries));
|
|
|
|
auto imported_libs = lib_elf.imported_libraries();
|
|
|
|
if (imported_libs.size() > kMaxWorkQueueSize - to_resolve.size()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Exceeded max entries pending resolving limit");
|
|
|
|
}
|
2019-05-11 00:40:42 +08:00
|
|
|
|
2023-08-18 18:11:57 +08:00
|
|
|
if (SAPI_RAW_VLOG_IS_ON(2)) {
|
2019-05-11 00:40:42 +08:00
|
|
|
SAPI_RAW_VLOG(2,
|
|
|
|
"Transitive dependencies of %s to resolve (depth = %d): ",
|
2020-12-14 19:33:29 +08:00
|
|
|
resolved_lib.c_str(), depth + 1);
|
2019-05-11 00:40:42 +08:00
|
|
|
LogContainer(imported_libs);
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
for (const auto& imported_lib : imported_libs) {
|
|
|
|
to_resolve.emplace_back(imported_lib, depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:11:19 +08:00
|
|
|
imported_libraries.insert(interpreter);
|
2019-03-19 00:21:48 +08:00
|
|
|
for (const auto& lib : imported_libraries) {
|
|
|
|
SAPI_RETURN_IF_ERROR(AddFile(lib));
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Mounts::AddTmpfs(absl::string_view inside, size_t sz) {
|
2019-03-19 00:21:48 +08:00
|
|
|
MountTree::Node node;
|
|
|
|
auto tmpfs_node = node.mutable_tmpfs_node();
|
|
|
|
tmpfs_node->set_tmpfs_options(absl::StrCat("size=", sz));
|
|
|
|
return Insert(inside, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
uint64_t GetMountFlagsFor(const std::string& path) {
|
|
|
|
struct statvfs vfs;
|
|
|
|
if (TEMP_FAILURE_RETRY(statvfs(path.c_str(), &vfs)) == -1) {
|
|
|
|
SAPI_RAW_PLOG(ERROR, "statvfs");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t flags = 0;
|
2021-05-17 19:06:39 +08:00
|
|
|
using MountPair = std::pair<uint64_t, uint64_t>;
|
|
|
|
for (const auto& [mount_flag, vfs_flag] : {
|
2022-03-08 15:57:28 +08:00
|
|
|
MountPair(MS_RDONLY, ST_RDONLY),
|
2021-05-17 19:06:39 +08:00
|
|
|
MountPair(MS_NOSUID, ST_NOSUID),
|
|
|
|
MountPair(MS_NODEV, ST_NODEV),
|
|
|
|
MountPair(MS_NOEXEC, ST_NOEXEC),
|
|
|
|
MountPair(MS_SYNCHRONOUS, ST_SYNCHRONOUS),
|
|
|
|
MountPair(MS_MANDLOCK, ST_MANDLOCK),
|
|
|
|
MountPair(MS_NOATIME, ST_NOATIME),
|
|
|
|
MountPair(MS_NODIRATIME, ST_NODIRATIME),
|
|
|
|
MountPair(MS_RELATIME, ST_RELATIME),
|
|
|
|
}) {
|
|
|
|
if (vfs.f_flag & vfs_flag) {
|
|
|
|
flags |= mount_flag;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2020-01-27 19:19:28 +08:00
|
|
|
std::string MountFlagsToString(uint64_t flags) {
|
|
|
|
#define SAPI_MAP(x) \
|
|
|
|
{ x, #x }
|
2021-05-17 19:06:39 +08:00
|
|
|
static constexpr std::pair<uint64_t, absl::string_view> kMap[] = {
|
|
|
|
SAPI_MAP(MS_RDONLY), SAPI_MAP(MS_NOSUID),
|
|
|
|
SAPI_MAP(MS_NODEV), SAPI_MAP(MS_NOEXEC),
|
|
|
|
SAPI_MAP(MS_SYNCHRONOUS), SAPI_MAP(MS_REMOUNT),
|
|
|
|
SAPI_MAP(MS_MANDLOCK), SAPI_MAP(MS_DIRSYNC),
|
|
|
|
SAPI_MAP(MS_NOATIME), SAPI_MAP(MS_NODIRATIME),
|
|
|
|
SAPI_MAP(MS_BIND), SAPI_MAP(MS_MOVE),
|
2020-01-27 19:19:28 +08:00
|
|
|
SAPI_MAP(MS_REC),
|
|
|
|
#ifdef MS_VERBOSE
|
2021-05-17 19:06:39 +08:00
|
|
|
SAPI_MAP(MS_VERBOSE), // Deprecated
|
2020-01-27 19:19:28 +08:00
|
|
|
#endif
|
2021-05-17 19:06:39 +08:00
|
|
|
SAPI_MAP(MS_SILENT), SAPI_MAP(MS_POSIXACL),
|
|
|
|
SAPI_MAP(MS_UNBINDABLE), SAPI_MAP(MS_PRIVATE),
|
2020-09-11 18:13:46 +08:00
|
|
|
SAPI_MAP(MS_SLAVE), // Inclusive language: system constant
|
2021-05-17 19:06:39 +08:00
|
|
|
SAPI_MAP(MS_SHARED), SAPI_MAP(MS_RELATIME),
|
|
|
|
SAPI_MAP(MS_KERNMOUNT), SAPI_MAP(MS_I_VERSION),
|
2020-01-27 19:19:28 +08:00
|
|
|
SAPI_MAP(MS_STRICTATIME),
|
|
|
|
#ifdef MS_LAZYTIME
|
2021-05-17 19:06:39 +08:00
|
|
|
SAPI_MAP(MS_LAZYTIME), // Added in Linux 4.0
|
2020-01-27 19:19:28 +08:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#undef SAPI_MAP
|
|
|
|
std::vector<absl::string_view> flags_list;
|
2021-05-17 19:06:39 +08:00
|
|
|
for (const auto& [val, str] : kMap) {
|
2020-01-27 19:19:28 +08:00
|
|
|
if ((flags & val) == val) {
|
|
|
|
flags &= ~val;
|
|
|
|
flags_list.push_back(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string flags_str = absl::StrCat(flags);
|
|
|
|
if (flags_list.empty() || flags != 0) {
|
|
|
|
flags_list.push_back(flags_str);
|
|
|
|
}
|
|
|
|
return absl::StrJoin(flags_list, "|");
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
void MountWithDefaults(const std::string& source, const std::string& target,
|
|
|
|
const char* fs_type, uint64_t extra_flags,
|
|
|
|
const char* option_str, bool is_ro) {
|
2020-01-16 22:04:44 +08:00
|
|
|
uint64_t flags = MS_REC | MS_NOSUID | extra_flags;
|
|
|
|
if (is_ro) {
|
|
|
|
flags |= MS_RDONLY;
|
|
|
|
}
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, R"(mount("%s", "%s", "%s", %s, "%s"))", source.c_str(),
|
|
|
|
target.c_str(), fs_type, MountFlagsToString(flags).c_str(),
|
|
|
|
option_str);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
int res = mount(source.c_str(), target.c_str(), fs_type, flags, option_str);
|
|
|
|
if (res == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
// File does not exist (anymore). This is e.g. the case when we're trying
|
|
|
|
// to gather stack-traces on SAPI crashes. The sandboxee application is a
|
|
|
|
// memfd file that is not existing anymore.
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(WARNING, "Could not mount %s: file does not exist",
|
|
|
|
source.c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-01-27 19:19:28 +08:00
|
|
|
SAPI_RAW_PLOG(FATAL, "mounting %s to %s failed (flags=%s)", source, target,
|
|
|
|
MountFlagsToString(flags));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 22:04:44 +08:00
|
|
|
// Flags are ignored for a bind mount, a remount is needed to set the flags.
|
|
|
|
if (extra_flags & MS_BIND) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// Get actual mount flags.
|
2022-03-08 15:57:28 +08:00
|
|
|
uint64_t target_flags = GetMountFlagsFor(target);
|
|
|
|
if ((target_flags & MS_RDONLY) != 0 && (flags & MS_RDONLY) == 0) {
|
|
|
|
SAPI_RAW_LOG(FATAL,
|
|
|
|
"cannot remount %s as read-write as it's on read-only dev",
|
|
|
|
target.c_str());
|
|
|
|
}
|
|
|
|
res = mount("", target.c_str(), "", flags | target_flags | MS_REMOUNT,
|
|
|
|
nullptr);
|
2020-01-27 19:19:28 +08:00
|
|
|
SAPI_RAW_PCHECK(res != -1, "remounting %s with flags=%s failed", target,
|
|
|
|
MountFlagsToString(flags));
|
2020-01-16 22:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount propagation has to be set separately
|
|
|
|
const uint64_t propagation =
|
|
|
|
extra_flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE);
|
|
|
|
if (propagation != 0) {
|
|
|
|
res = mount("", target.c_str(), "", propagation, nullptr);
|
2020-01-27 19:19:28 +08:00
|
|
|
SAPI_RAW_PCHECK(res != -1, "changing %s mount propagation to %s failed",
|
2020-12-14 19:33:29 +08:00
|
|
|
target, MountFlagsToString(propagation).c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverses the MountTree to create all required files and perform the mounts.
|
|
|
|
void CreateMounts(const MountTree& tree, const std::string& path,
|
|
|
|
bool create_backing_files) {
|
|
|
|
// First, create the backing files if needed.
|
|
|
|
if (create_backing_files) {
|
|
|
|
switch (tree.node().node_case()) {
|
|
|
|
case MountTree::Node::kFileNode: {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(2, "Creating backing file at %s", path.c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
int fd = open(path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0600);
|
|
|
|
SAPI_RAW_PCHECK(fd != -1, "");
|
|
|
|
SAPI_RAW_PCHECK(close(fd) == 0, "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MountTree::Node::kDirNode:
|
|
|
|
case MountTree::Node::kTmpfsNode:
|
2019-11-14 19:51:01 +08:00
|
|
|
case MountTree::Node::kRootNode:
|
2019-03-19 00:21:48 +08:00
|
|
|
case MountTree::Node::NODE_NOT_SET:
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(2, "Creating directory at %s", path.c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RAW_PCHECK(mkdir(path.c_str(), 0700) == 0 || errno == EEXIST, "");
|
|
|
|
break;
|
|
|
|
// Intentionally no default to make sure we handle all the cases.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the actual mounts based on the node type.
|
|
|
|
switch (tree.node().node_case()) {
|
|
|
|
case MountTree::Node::kDirNode: {
|
|
|
|
// Since this directory is bind mounted, it's the users
|
|
|
|
// responsibility to make sure that all backing files are in place.
|
|
|
|
create_backing_files = false;
|
|
|
|
|
|
|
|
auto node = tree.node().dir_node();
|
|
|
|
MountWithDefaults(node.outside(), path, "", MS_BIND, nullptr,
|
2022-03-14 20:14:41 +08:00
|
|
|
!node.writable());
|
2019-03-19 00:21:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MountTree::Node::kTmpfsNode: {
|
|
|
|
// We can always create backing files under a tmpfs.
|
|
|
|
create_backing_files = true;
|
|
|
|
|
|
|
|
auto node = tree.node().tmpfs_node();
|
|
|
|
MountWithDefaults("", path, "tmpfs", 0, node.tmpfs_options().c_str(),
|
|
|
|
/* is_ro */ false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MountTree::Node::kFileNode: {
|
|
|
|
auto node = tree.node().file_node();
|
|
|
|
MountWithDefaults(node.outside(), path, "", MS_BIND, nullptr,
|
2022-03-14 20:14:41 +08:00
|
|
|
!node.writable());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// A file node has to be a leaf so we can skip traversing here.
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 19:51:01 +08:00
|
|
|
case MountTree::Node::kRootNode:
|
2019-03-19 00:21:48 +08:00
|
|
|
case MountTree::Node::NODE_NOT_SET:
|
|
|
|
// Nothing to do, we already created the directory above.
|
|
|
|
break;
|
|
|
|
// Intentionally no default to make sure we handle all the cases.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse the subtrees.
|
|
|
|
for (const auto& kv : tree.entries()) {
|
2021-02-19 16:43:06 +08:00
|
|
|
std::string new_path = sapi::file::JoinPath(path, kv.first);
|
2019-03-19 00:21:48 +08:00
|
|
|
CreateMounts(kv.second, new_path, create_backing_files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Mounts::CreateMounts(const std::string& root_path) const {
|
|
|
|
sandbox2::CreateMounts(mount_tree_, root_path, true);
|
|
|
|
}
|
|
|
|
|
2019-05-08 09:29:51 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void RecursivelyListMountsImpl(const MountTree& tree,
|
|
|
|
const std::string& tree_path,
|
|
|
|
std::vector<std::string>* outside_entries,
|
|
|
|
std::vector<std::string>* inside_entries) {
|
|
|
|
const MountTree::Node& node = tree.node();
|
|
|
|
if (node.has_dir_node()) {
|
2022-03-14 20:14:41 +08:00
|
|
|
const char* rw_str = node.dir_node().writable() ? "W " : "R ";
|
2019-05-08 09:29:51 +08:00
|
|
|
inside_entries->emplace_back(absl::StrCat(rw_str, tree_path, "/"));
|
|
|
|
outside_entries->emplace_back(absl::StrCat(node.dir_node().outside(), "/"));
|
|
|
|
} else if (node.has_file_node()) {
|
2022-03-14 20:14:41 +08:00
|
|
|
const char* rw_str = node.file_node().writable() ? "W " : "R ";
|
2019-05-08 09:29:51 +08:00
|
|
|
inside_entries->emplace_back(absl::StrCat(rw_str, tree_path));
|
|
|
|
outside_entries->emplace_back(absl::StrCat(node.file_node().outside()));
|
|
|
|
} else if (node.has_tmpfs_node()) {
|
2019-10-24 17:37:28 +08:00
|
|
|
inside_entries->emplace_back(tree_path);
|
2019-05-08 09:29:51 +08:00
|
|
|
outside_entries->emplace_back(
|
|
|
|
absl::StrCat("tmpfs: ", node.tmpfs_node().tmpfs_options()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& subentry : tree.entries()) {
|
|
|
|
RecursivelyListMountsImpl(subentry.second,
|
|
|
|
absl::StrCat(tree_path, "/", subentry.first),
|
|
|
|
outside_entries, inside_entries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-08-03 22:39:37 +08:00
|
|
|
void Mounts::RecursivelyListMounts(
|
|
|
|
std::vector<std::string>* outside_entries,
|
|
|
|
std::vector<std::string>* inside_entries) const {
|
2019-05-08 09:29:51 +08:00
|
|
|
RecursivelyListMountsImpl(GetMountTree(), "", outside_entries,
|
|
|
|
inside_entries);
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace sandbox2
|