xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/mounts.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #ifndef SANDBOXED_API_SANDBOX2_MOUNTTREE_H_
16*ec63e07aSXin Li #define SANDBOXED_API_SANDBOX2_MOUNTTREE_H_
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #include <cstddef>
19*ec63e07aSXin Li #include <string>
20*ec63e07aSXin Li #include <utility>
21*ec63e07aSXin Li #include <vector>
22*ec63e07aSXin Li 
23*ec63e07aSXin Li #include "absl/status/status.h"
24*ec63e07aSXin Li #include "absl/status/statusor.h"
25*ec63e07aSXin Li #include "absl/strings/string_view.h"
26*ec63e07aSXin Li #include "sandboxed_api/sandbox2/mount_tree.pb.h"
27*ec63e07aSXin Li 
28*ec63e07aSXin Li namespace sandbox2 {
29*ec63e07aSXin Li 
30*ec63e07aSXin Li namespace internal {
31*ec63e07aSXin Li 
32*ec63e07aSXin Li bool IsSameFile(const std::string& path1, const std::string& path2);
33*ec63e07aSXin Li bool IsWritable(const MountTree::Node& node);
34*ec63e07aSXin Li bool HasSameTarget(const MountTree::Node& n1, const MountTree::Node& n2);
35*ec63e07aSXin Li bool IsEquivalentNode(const MountTree::Node& n1, const MountTree::Node& n2);
36*ec63e07aSXin Li }  // namespace internal
37*ec63e07aSXin Li 
38*ec63e07aSXin Li class Mounts {
39*ec63e07aSXin Li  public:
Mounts()40*ec63e07aSXin Li   Mounts() {
41*ec63e07aSXin Li     MountTree::Node root;
42*ec63e07aSXin Li     root.mutable_root_node()->set_writable(false);
43*ec63e07aSXin Li     *mount_tree_.mutable_node() = root;
44*ec63e07aSXin Li   }
45*ec63e07aSXin Li 
Mounts(MountTree mount_tree)46*ec63e07aSXin Li   explicit Mounts(MountTree mount_tree) : mount_tree_(std::move(mount_tree)) {}
47*ec63e07aSXin Li 
48*ec63e07aSXin Li   Mounts(const Mounts&) = default;
49*ec63e07aSXin Li   Mounts(Mounts&&) = default;
50*ec63e07aSXin Li   Mounts& operator=(const Mounts&) = default;
51*ec63e07aSXin Li   Mounts& operator=(Mounts&&) = default;
52*ec63e07aSXin Li 
53*ec63e07aSXin Li   absl::Status AddFile(absl::string_view path, bool is_ro = true) {
54*ec63e07aSXin Li     return AddFileAt(path, path, is_ro);
55*ec63e07aSXin Li   }
56*ec63e07aSXin Li 
57*ec63e07aSXin Li   absl::Status AddFileAt(absl::string_view outside, absl::string_view inside,
58*ec63e07aSXin Li                          bool is_ro = true);
59*ec63e07aSXin Li 
60*ec63e07aSXin Li   absl::Status AddDirectory(absl::string_view path, bool is_ro = true) {
61*ec63e07aSXin Li     return AddDirectoryAt(path, path, is_ro);
62*ec63e07aSXin Li   }
63*ec63e07aSXin Li 
64*ec63e07aSXin Li   absl::Status AddDirectoryAt(absl::string_view outside,
65*ec63e07aSXin Li                               absl::string_view inside, bool is_ro = true);
66*ec63e07aSXin Li 
67*ec63e07aSXin Li   absl::Status AddMappingsForBinary(const std::string& path,
68*ec63e07aSXin Li                                     absl::string_view ld_library_path = {});
69*ec63e07aSXin Li 
70*ec63e07aSXin Li   absl::Status AddTmpfs(absl::string_view inside, size_t sz);
71*ec63e07aSXin Li 
72*ec63e07aSXin Li   absl::Status Remove(absl::string_view path);
73*ec63e07aSXin Li 
74*ec63e07aSXin Li   void CreateMounts(const std::string& root_path) const;
75*ec63e07aSXin Li 
GetMountTree()76*ec63e07aSXin Li   MountTree GetMountTree() const { return mount_tree_; }
77*ec63e07aSXin Li 
SetRootWritable()78*ec63e07aSXin Li   void SetRootWritable() {
79*ec63e07aSXin Li     mount_tree_.mutable_node()->mutable_root_node()->set_writable(true);
80*ec63e07aSXin Li   }
81*ec63e07aSXin Li 
IsRootReadOnly()82*ec63e07aSXin Li   bool IsRootReadOnly() const {
83*ec63e07aSXin Li     return mount_tree_.has_node() && mount_tree_.node().has_root_node() &&
84*ec63e07aSXin Li            !mount_tree_.node().root_node().writable();
85*ec63e07aSXin Li   }
86*ec63e07aSXin Li 
87*ec63e07aSXin Li   // Lists the outside and inside entries of the input tree in the output
88*ec63e07aSXin Li   // parameters, in an ls-like manner. Each entry is traversed in the
89*ec63e07aSXin Li   // depth-first order. However, the entries on the same level of hierarchy are
90*ec63e07aSXin Li   // traversed in their natural order in the tree. The elements in the output
91*ec63e07aSXin Li   // containers match each other pairwise: outside_entries[i] is mounted as
92*ec63e07aSXin Li   // inside_entries[i]. The elements of inside_entries are prefixed with either
93*ec63e07aSXin Li   // 'R' (read-only) or 'W' (writable).
94*ec63e07aSXin Li   void RecursivelyListMounts(std::vector<std::string>* outside_entries,
95*ec63e07aSXin Li                              std::vector<std::string>* inside_entries) const;
96*ec63e07aSXin Li 
97*ec63e07aSXin Li   absl::StatusOr<std::string> ResolvePath(absl::string_view path) const;
98*ec63e07aSXin Li 
99*ec63e07aSXin Li  private:
100*ec63e07aSXin Li   friend class MountTreeTest;
101*ec63e07aSXin Li 
102*ec63e07aSXin Li   absl::Status Insert(absl::string_view path, const MountTree::Node& node);
103*ec63e07aSXin Li 
104*ec63e07aSXin Li   MountTree mount_tree_;
105*ec63e07aSXin Li };
106*ec63e07aSXin Li 
107*ec63e07aSXin Li }  // namespace sandbox2
108*ec63e07aSXin Li 
109*ec63e07aSXin Li #endif  // SANDBOXED_API_SANDBOX2_MOUNTTREE_H_
110