1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "host/commands/process_sandboxer/filesystem.h"
16 
17 #include <sys/stat.h>
18 
19 #include <deque>
20 #include <initializer_list>
21 #include <string>
22 #include <string_view>
23 
24 #include <absl/strings/str_cat.h>
25 #include <absl/strings/str_join.h>
26 #include <absl/strings/str_split.h>
27 #include <absl/strings/strip.h>
28 
29 namespace cuttlefish::process_sandboxer {
30 
31 // Copied from sandboxed_api/util/path.cc
32 
33 namespace internal {
34 
35 constexpr char kPathSeparator[] = "/";
36 
JoinPathImpl(std::initializer_list<absl::string_view> paths)37 std::string JoinPathImpl(std::initializer_list<absl::string_view> paths) {
38   std::string result;
39   for (const auto& path : paths) {
40     if (path.empty()) {
41       continue;
42     }
43     if (result.empty()) {
44       absl::StrAppend(&result, path);
45       continue;
46     }
47     const auto comp = absl::StripPrefix(path, kPathSeparator);
48     if (absl::EndsWith(result, kPathSeparator)) {
49       absl::StrAppend(&result, comp);
50     } else {
51       absl::StrAppend(&result, kPathSeparator, comp);
52     }
53   }
54   return result;
55 }
56 
57 }  // namespace internal
58 
59 // Copied from sandboxed_api/util/fileops.cc
60 
61 namespace {
62 
StripBasename(std::string_view path)63 std::string StripBasename(std::string_view path) {
64   const auto last_slash = path.find_last_of('/');
65   if (last_slash == std::string::npos) {
66     return "";
67   }
68   if (last_slash == 0) {
69     return "/";
70   }
71   return std::string(path.substr(0, last_slash));
72 }
73 
74 }  // namespace
75 
CreateDirectoryRecursively(const std::string & path,int mode)76 bool CreateDirectoryRecursively(const std::string& path, int mode) {
77   if (mkdir(path.c_str(), mode) == 0 || errno == EEXIST) {
78     return true;
79   }
80 
81   // We couldn't create the dir for reasons we can't handle.
82   if (errno != ENOENT) {
83     return false;
84   }
85 
86   // The ENOENT case, the parent directory doesn't exist yet.
87   // Let's create it.
88   const std::string dir = StripBasename(path);
89   if (dir == "/" || dir.empty()) {
90     return false;
91   }
92   if (!CreateDirectoryRecursively(dir, mode)) {
93     return false;
94   }
95 
96   // Now the parent dir exists, retry creating the directory.
97   return mkdir(path.c_str(), mode) == 0;
98 }
99 
CleanPath(const std::string_view unclean_path)100 std::string CleanPath(const std::string_view unclean_path) {
101   int dotdot_num = 0;
102   std::deque<absl::string_view> parts;
103   for (absl::string_view part :
104        absl::StrSplit(unclean_path, '/', absl::SkipEmpty())) {
105     if (part == "..") {
106       if (parts.empty()) {
107         ++dotdot_num;
108       } else {
109         parts.pop_back();
110       }
111     } else if (part != ".") {
112       parts.push_back(part);
113     }
114   }
115   if (absl::StartsWith(unclean_path, "/")) {
116     if (parts.empty()) {
117       return "/";
118     }
119     parts.push_front("");
120   } else {
121     for (; dotdot_num; --dotdot_num) {
122       parts.push_front("..");
123     }
124     if (parts.empty()) {
125       return ".";
126     }
127   }
128   return absl::StrJoin(parts, "/");
129 }
130 
131 }  // namespace cuttlefish::process_sandboxer
132