1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "berberis/kernel_api/open_emulation.h"
18
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 #include <cstdio>
24 #include <cstring>
25 #include <mutex>
26 #include <utility>
27
28 #include "berberis/base/arena_alloc.h"
29 #include "berberis/base/arena_map.h"
30 #include "berberis/base/arena_string.h"
31 #include "berberis/base/arena_vector.h"
32 #include "berberis/base/checks.h"
33 #include "berberis/base/fd.h"
34 #include "berberis/base/forever_alloc.h"
35 #include "berberis/base/tracing.h"
36 #include "berberis/guest_os_primitives/guest_map_shadow.h"
37 #include "berberis/guest_state/guest_addr.h"
38 #include "berberis/kernel_api/main_executable_real_path_emulation.h"
39
40 namespace berberis {
41
42 namespace {
43
44 class EmulatedFileDescriptors {
45 public:
EmulatedFileDescriptors()46 explicit EmulatedFileDescriptors() : fds_(&arena_) {}
47
GetInstance()48 static EmulatedFileDescriptors* GetInstance() {
49 static auto* g_emulated_proc_self_maps_fds = NewForever<EmulatedFileDescriptors>();
50 return g_emulated_proc_self_maps_fds;
51 }
52
53 // Not copyable or movable.
54 EmulatedFileDescriptors(const EmulatedFileDescriptors&) = delete;
55 EmulatedFileDescriptors& operator=(const EmulatedFileDescriptors&) = delete;
56
Add(int fd)57 void Add(int fd) {
58 std::lock_guard lock(mutex_);
59 auto [unused_it, inserted] = fds_.insert(std::make_pair(fd, 0));
60 if (!inserted) {
61 // We expect every fd to be added at most once. But if it breaks let's consider it non-fatal.
62 TRACE("Detected duplicated fd in EmulatedFileDescriptors");
63 }
64 }
65
Contains(int fd)66 bool Contains(int fd) {
67 std::lock_guard lock(mutex_);
68 return fds_.find(fd) != fds_.end();
69 }
70
Remove(int fd)71 void Remove(int fd) {
72 std::lock_guard lock(mutex_);
73 auto it = fds_.find(fd);
74 if (it != fds_.end()) {
75 fds_.erase(it);
76 }
77 }
78
79 private:
80 std::mutex mutex_;
81 Arena arena_;
82 // We use it as a set because we don't have ArenaSet, so client data isn't really used.
83 ArenaMap<int, int> fds_;
84 };
85
86 // It's macro since we use it as string literal below.
87 #define PROC_SELF_MAPS "/proc/self/maps"
88
89 // Reader that works with custom allocator strings. Based on android::base::ReadFileToString.
90 template <typename String>
ReadProcSelfMapsToString(String & content)91 bool ReadProcSelfMapsToString(String& content) {
92 int fd = open(PROC_SELF_MAPS, O_RDONLY);
93 if (fd == -1) {
94 return false;
95 }
96 char buf[4096] __attribute__((__uninitialized__));
97 ssize_t n;
98 while ((n = read(fd, &buf[0], sizeof(buf))) > 0) {
99 content.append(buf, n);
100 }
101 close(fd);
102 return n == 0;
103 }
104
105 // String split that works with custom allocator strings. Based on android::base::Split.
106 template <typename String>
SplitLines(Arena * arena,const String & content)107 ArenaVector<String> SplitLines(Arena* arena, const String& content) {
108 ArenaVector<String> lines(arena);
109 size_t base = 0;
110 size_t found;
111 while (true) {
112 found = content.find_first_of('\n', base);
113 lines.emplace_back(content, base, found - base, content.get_allocator());
114 if (found == content.npos) break;
115 base = found + 1;
116 }
117 return lines;
118 }
119
120 // Note that dirfd, flags and mode are only used to fallback to
121 // host's openat in case of failure.
122 // Avoid mallocs since bionic tests use it under malloc_disable (b/338211718).
OpenatProcSelfMapsForGuest(int dirfd,int flags,mode_t mode)123 int OpenatProcSelfMapsForGuest(int dirfd, int flags, mode_t mode) {
124 TRACE("Openat for " PROC_SELF_MAPS);
125
126 Arena arena;
127 ArenaString file_data(&arena);
128 bool success = ReadProcSelfMapsToString(file_data);
129 if (!success) {
130 TRACE("Cannot read " PROC_SELF_MAPS ", falling back to host's openat");
131 return openat(dirfd, PROC_SELF_MAPS, flags, mode);
132 }
133
134 int mem_fd = CreateMemfdOrDie("[guest " PROC_SELF_MAPS "]");
135
136 auto* maps_shadow = GuestMapShadow::GetInstance();
137
138 auto lines = SplitLines(&arena, file_data);
139 ArenaString guest_maps(&arena);
140 for (size_t i = 0; i < lines.size(); i++) {
141 uintptr_t start;
142 uintptr_t end;
143 int prot_offset;
144 if (sscanf(lines.at(i).c_str(), "%" SCNxPTR "-%" SCNxPTR " %n", &start, &end, &prot_offset) !=
145 2) {
146 if (!lines[i].empty()) {
147 TRACE("Cannot parse " PROC_SELF_MAPS " line : %s", lines.at(i).c_str());
148 }
149 guest_maps.append(lines.at(i) + "\n");
150 continue;
151 }
152 BitValue exec_status = maps_shadow->GetExecutable(GuestAddr(start), end - start);
153 if (exec_status == kBitMixed) {
154 // When we strip guest executable bit from host mappings the kernel may merge r-- and r-x
155 // mappings, resulting in kBitMixed executability state. We are avoiding such merging by
156 // SetVmaAnonName in MmapForGuest/MprotectForGuest. This isn't strictly guaranteed to work, so
157 // issue a warning if it doesn't, or if we got kBitMixed for another reason to investigate.
158 // TODO(b/322873334): Instead split such host mapping into several guest mappings.
159 TRACE("Unexpected " PROC_SELF_MAPS " mapping with mixed guest executability");
160 }
161 // prot_offset points to "rwxp", so offset of "x" is 2 symbols away.
162 lines.at(i).at(prot_offset + 2) = (exec_status == kBitSet) ? 'x' : '-';
163
164 guest_maps.append(lines.at(i) + "\n");
165 }
166
167 // Normally /proc/self/maps doesn't have newline at the end.
168 // It's simpler to remove it than to not add it in the loop.
169 CHECK_EQ(guest_maps.back(), '\n');
170 guest_maps.pop_back();
171
172 TRACE("--------\n%s\n--------", guest_maps.c_str());
173
174 WriteFullyOrDie(mem_fd, guest_maps.c_str(), guest_maps.size());
175
176 lseek(mem_fd, 0, 0);
177
178 EmulatedFileDescriptors::GetInstance()->Add(mem_fd);
179
180 return mem_fd;
181 }
182
IsProcSelfMaps(const char * path,int flags)183 bool IsProcSelfMaps(const char* path, int flags) {
184 struct stat cur_stat;
185 struct stat proc_stat;
186 // This check works for /proc/self/maps itself as well as symlinks (unless AT_SYMLINK_NOFOLLOW is
187 // requested). As an added benefit it gracefully handles invalid pointers in path.
188 return stat(path, &cur_stat) == 0 && stat(PROC_SELF_MAPS, &proc_stat) == 0 &&
189 !(S_ISLNK(cur_stat.st_mode) && (flags & AT_SYMLINK_NOFOLLOW)) &&
190 cur_stat.st_ino == proc_stat.st_ino && cur_stat.st_dev == proc_stat.st_dev;
191 }
192
193 // In zygote this is not needed because native bridge is mounting
194 // /proc/cpuinfo to point to the emulated version. But for executables
195 // this does not happen and they end up reading host cpuinfo.
196 //
197 // Note that current selinux policies prevent us from mounting /proc/cpuinfo
198 // (replicating what zygote process does) for executables hence we need to
199 // emulate it here.
TryTranslateProcCpuinfoPath(const char * path,int flags)200 const char* TryTranslateProcCpuinfoPath(const char* path, int flags) {
201 #if defined(__ANDROID__)
202 struct stat cur_stat;
203 struct stat cpuinfo_stat;
204
205 if (stat(path, &cur_stat) == -1 || stat("/proc/cpuinfo", &cpuinfo_stat) == -1) {
206 return nullptr;
207 }
208
209 if (S_ISLNK(cur_stat.st_mode) && (flags & AT_SYMLINK_NOFOLLOW)) {
210 return nullptr;
211 }
212
213 if ((cur_stat.st_ino == cpuinfo_stat.st_ino) && (cur_stat.st_dev == cpuinfo_stat.st_dev)) {
214 TRACE("openat: Translating %s to %s", path, kGuestCpuinfoPath);
215 return kGuestCpuinfoPath;
216 }
217 #else
218 UNUSED(path, flags);
219 #endif
220 return nullptr;
221 }
222
223 } // namespace
224
IsFileDescriptorEmulatedProcSelfMaps(int fd)225 bool IsFileDescriptorEmulatedProcSelfMaps(int fd) {
226 return EmulatedFileDescriptors::GetInstance()->Contains(fd);
227 }
228
CloseEmulatedProcSelfMapsFileDescriptor(int fd)229 void CloseEmulatedProcSelfMapsFileDescriptor(int fd) {
230 EmulatedFileDescriptors::GetInstance()->Remove(fd);
231 }
232
OpenatForGuest(int dirfd,const char * path,int guest_flags,mode_t mode)233 int OpenatForGuest(int dirfd, const char* path, int guest_flags, mode_t mode) {
234 int host_flags = ToHostOpenFlags(guest_flags);
235
236 if (IsProcSelfMaps(path, host_flags)) {
237 return OpenatProcSelfMapsForGuest(dirfd, host_flags, mode);
238 }
239
240 const char* real_path = nullptr;
241 if ((host_flags & AT_SYMLINK_NOFOLLOW) == 0) {
242 real_path = TryReadLinkToMainExecutableRealPath(path);
243 }
244
245 if (real_path == nullptr) {
246 real_path = TryTranslateProcCpuinfoPath(path, host_flags);
247 }
248
249 return openat(dirfd, real_path != nullptr ? real_path : path, host_flags, mode);
250 }
251
OpenForGuest(const char * path,int flags,mode_t mode)252 int OpenForGuest(const char* path, int flags, mode_t mode) {
253 return OpenatForGuest(AT_FDCWD, path, flags, mode);
254 }
255
256 } // namespace berberis
257