1 /*
2  * Copyright (C) 2021 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 "androidfw/AssetsProvider.h"
18 
19 #include <sys/stat.h>
20 
21 #include <android-base/errors.h>
22 #include <android-base/stringprintf.h>
23 #include <android-base/utf8.h>
24 #include <ziparchive/zip_archive.h>
25 
26 namespace android {
27 namespace {
28 constexpr const char* kEmptyDebugString = "<empty>";
29 } // namespace
30 
Open(const std::string & path,Asset::AccessMode mode,bool * file_exists) const31 std::unique_ptr<Asset> AssetsProvider::Open(const std::string& path, Asset::AccessMode mode,
32                                             bool* file_exists) const {
33   return OpenInternal(path, mode, file_exists);
34 }
35 
CreateAssetFromFile(const std::string & path)36 std::unique_ptr<Asset> AssetsProvider::CreateAssetFromFile(const std::string& path) {
37   base::unique_fd fd(base::utf8::open(path.c_str(), O_RDONLY | O_CLOEXEC));
38   if (!fd.ok()) {
39     LOG(ERROR) << "Failed to open file '" << path << "': " << base::SystemErrorCodeToString(errno);
40     return {};
41   }
42 
43   return CreateAssetFromFd(std::move(fd), path.c_str());
44 }
45 
CreateAssetFromFd(base::unique_fd fd,const char * path,off64_t offset,off64_t length)46 std::unique_ptr<Asset> AssetsProvider::CreateAssetFromFd(base::unique_fd fd,
47                                                          const char* path,
48                                                          off64_t offset,
49                                                          off64_t length) {
50   CHECK(length >= kUnknownLength) << "length must be greater than or equal to " << kUnknownLength;
51   CHECK(length != kUnknownLength || offset == 0) << "offset must be 0 if length is "
52                                                  << kUnknownLength;
53   if (length == kUnknownLength) {
54     length = lseek64(fd, 0, SEEK_END);
55     if (length < 0) {
56       LOG(ERROR) << "Failed to get size of file '" << ((path) ? path : "anon") << "': "
57                  << base::SystemErrorCodeToString(errno);
58       return {};
59     }
60   }
61 
62   incfs::IncFsFileMap file_map;
63   if (!file_map.Create(fd, offset, static_cast<size_t>(length), path)) {
64     LOG(ERROR) << "Failed to mmap file '" << ((path != nullptr) ? path : "anon") << "': "
65                << base::SystemErrorCodeToString(errno);
66     return {};
67   }
68 
69   // If `path` is set, do not pass ownership of the `fd` to the new Asset since
70   // Asset::openFileDescriptor can use `path` to create new file descriptors.
71   return Asset::createFromUncompressedMap(std::move(file_map),
72                                           Asset::AccessMode::ACCESS_RANDOM,
73                                           (path != nullptr) ? base::unique_fd(-1) : std::move(fd));
74 }
75 
GetPath() const76 const std::string* ZipAssetsProvider::PathOrDebugName::GetPath() const {
77   return is_path_ ? &value_ : nullptr;
78 }
79 
GetDebugName() const80 const std::string& ZipAssetsProvider::PathOrDebugName::GetDebugName() const {
81   return value_;
82 }
83 
operator ()(ZipArchive * a) const84 void ZipAssetsProvider::ZipCloser::operator()(ZipArchive* a) const {
85   ::CloseArchive(a);
86 }
87 
ZipAssetsProvider(ZipArchiveHandle handle,PathOrDebugName && path,package_property_t flags,time_t last_mod_time)88 ZipAssetsProvider::ZipAssetsProvider(ZipArchiveHandle handle, PathOrDebugName&& path,
89                                      package_property_t flags, time_t last_mod_time)
90     : zip_handle_(handle),
91       name_(std::move(path)),
92       flags_(flags),
93       last_mod_time_(last_mod_time) {}
94 
Create(std::string path,package_property_t flags,base::unique_fd fd)95 std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(std::string path,
96                                                              package_property_t flags,
97                                                              base::unique_fd fd) {
98   const auto released_fd = fd.ok() ? fd.release() : -1;
99   ZipArchiveHandle handle;
100   if (int32_t result = released_fd < 0 ? OpenArchive(path.c_str(), &handle)
101                                        : OpenArchiveFd(released_fd, path.c_str(), &handle)) {
102     LOG(ERROR) << "Failed to open APK '" << path << "': " << ::ErrorCodeString(result);
103     CloseArchive(handle);
104     return {};
105   }
106 
107   struct stat sb{.st_mtime = -1};
108   // Skip all up-to-date checks if the file won't ever change.
109   if (!isReadonlyFilesystem(path.c_str())) {
110     if ((released_fd < 0 ? stat(path.c_str(), &sb) : fstat(released_fd, &sb)) < 0) {
111       // Stat requires execute permissions on all directories path to the file. If the process does
112       // not have execute permissions on this file, allow the zip to be opened but IsUpToDate() will
113       // always have to return true.
114       PLOG(WARNING) << "Failed to stat file '" << path << "'";
115     }
116   }
117 
118   return std::unique_ptr<ZipAssetsProvider>(
119       new ZipAssetsProvider(handle, PathOrDebugName::Path(std::move(path)), flags, sb.st_mtime));
120 }
121 
Create(base::unique_fd fd,std::string friendly_name,package_property_t flags,off64_t offset,off64_t len)122 std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(base::unique_fd fd,
123                                                              std::string friendly_name,
124                                                              package_property_t flags,
125                                                              off64_t offset,
126                                                              off64_t len) {
127   ZipArchiveHandle handle;
128   const int released_fd = fd.release();
129   const int32_t result = (len == AssetsProvider::kUnknownLength)
130       ? ::OpenArchiveFd(released_fd, friendly_name.c_str(), &handle)
131       : ::OpenArchiveFdRange(released_fd, friendly_name.c_str(), &handle, len, offset);
132 
133   if (result != 0) {
134     LOG(ERROR) << "Failed to open APK '" << friendly_name << "' through FD with offset " << offset
135                << " and length " << len << ": " << ::ErrorCodeString(result);
136     CloseArchive(handle);
137     return {};
138   }
139 
140   struct stat sb{.st_mtime = -1};
141   // Skip all up-to-date checks if the file won't ever change.
142   if (!isReadonlyFilesystem(released_fd)) {
143     if (fstat(released_fd, &sb) < 0) {
144       // Stat requires execute permissions on all directories path to the file. If the process does
145       // not have execute permissions on this file, allow the zip to be opened but IsUpToDate() will
146       // always have to return true.
147       LOG(WARNING) << "Failed to fstat file '" << friendly_name
148                    << "': " << base::SystemErrorCodeToString(errno);
149     }
150   }
151 
152   return std::unique_ptr<ZipAssetsProvider>(new ZipAssetsProvider(
153       handle, PathOrDebugName::DebugName(std::move(friendly_name)), flags, sb.st_mtime));
154 }
155 
OpenInternal(const std::string & path,Asset::AccessMode mode,bool * file_exists) const156 std::unique_ptr<Asset> ZipAssetsProvider::OpenInternal(const std::string& path,
157                                                        Asset::AccessMode mode,
158                                                        bool* file_exists) const {
159     if (file_exists != nullptr) {
160       *file_exists = false;
161     }
162 
163     ZipEntry entry;
164     if (FindEntry(zip_handle_.get(), path, &entry) != 0) {
165       return {};
166     }
167 
168     if (file_exists != nullptr) {
169       *file_exists = true;
170     }
171 
172     const int fd = GetFileDescriptor(zip_handle_.get());
173     const off64_t fd_offset = GetFileDescriptorOffset(zip_handle_.get());
174     const bool incremental_hardening = (flags_ & PROPERTY_DISABLE_INCREMENTAL_HARDENING) == 0U;
175     incfs::IncFsFileMap asset_map;
176     if (entry.method == kCompressDeflated) {
177       if (!asset_map.Create(fd, entry.offset + fd_offset, entry.compressed_length,
178                             name_.GetDebugName().c_str(), incremental_hardening)) {
179         LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName()
180                    << "'";
181         return {};
182       }
183 
184       std::unique_ptr<Asset> asset =
185           Asset::createFromCompressedMap(std::move(asset_map), entry.uncompressed_length, mode);
186       if (asset == nullptr) {
187         LOG(ERROR) << "Failed to decompress '" << path << "' in APK '" << name_.GetDebugName()
188                    << "'";
189         return {};
190       }
191       return asset;
192     }
193 
194     if (!asset_map.Create(fd, entry.offset + fd_offset, entry.uncompressed_length,
195                           name_.GetDebugName().c_str(), incremental_hardening)) {
196       LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName() << "'";
197       return {};
198     }
199 
200     base::unique_fd ufd;
201     if (name_.GetPath() == nullptr) {
202       // If the zip name does not represent a path, create a new `fd` for the new Asset to own in
203       // order to create new file descriptors using Asset::openFileDescriptor. If the zip name is a
204       // path, it will be used to create new file descriptors.
205       ufd = base::unique_fd(dup(fd));
206       if (!ufd.ok()) {
207         LOG(ERROR) << "Unable to dup fd '" << path << "' in APK '" << name_.GetDebugName() << "'";
208         return {};
209       }
210     }
211 
212     auto asset = Asset::createFromUncompressedMap(std::move(asset_map), mode, std::move(ufd));
213     if (asset == nullptr) {
214       LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName() << "'";
215       return {};
216     }
217     return asset;
218 }
219 
ForEachFile(const std::string & root_path,base::function_ref<void (StringPiece,FileType)> f) const220 bool ZipAssetsProvider::ForEachFile(
221     const std::string& root_path,
222     base::function_ref<void(StringPiece, FileType)> f) const {
223     std::string root_path_full = root_path;
224     if (root_path_full.back() != '/') {
225       root_path_full += '/';
226     }
227 
228     void* cookie;
229     if (StartIteration(zip_handle_.get(), &cookie, root_path_full, "") != 0) {
230       return false;
231     }
232 
233     std::string name;
234     ::ZipEntry entry{};
235 
236     // We need to hold back directories because many paths will contain them and we want to only
237     // surface one.
238     std::set<std::string> dirs{};
239 
240     int32_t result;
241     while ((result = Next(cookie, &entry, &name)) == 0) {
242       StringPiece full_file_path(name);
243       StringPiece leaf_file_path = full_file_path.substr(root_path_full.size());
244 
245       if (!leaf_file_path.empty()) {
246         auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/');
247         if (iter != leaf_file_path.end()) {
248           std::string dir(leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter)));
249           dirs.insert(std::move(dir));
250         } else {
251           f(leaf_file_path, kFileTypeRegular);
252         }
253       }
254     }
255     EndIteration(cookie);
256 
257     // Now present the unique directories.
258     for (const std::string& dir : dirs) {
259       f(dir, kFileTypeDirectory);
260     }
261 
262     // -1 is end of iteration, anything else is an error.
263     return result == -1;
264 }
265 
GetCrc(std::string_view path) const266 std::optional<uint32_t> ZipAssetsProvider::GetCrc(std::string_view path) const {
267   ::ZipEntry entry;
268   if (FindEntry(zip_handle_.get(), path, &entry) != 0) {
269     return {};
270   }
271   return entry.crc32;
272 }
273 
GetPath() const274 std::optional<std::string_view> ZipAssetsProvider::GetPath() const {
275   if (name_.GetPath() != nullptr) {
276     return *name_.GetPath();
277   }
278   return {};
279 }
280 
GetDebugName() const281 const std::string& ZipAssetsProvider::GetDebugName() const {
282   return name_.GetDebugName();
283 }
284 
IsUpToDate() const285 bool ZipAssetsProvider::IsUpToDate() const {
286   if (last_mod_time_ == -1) {
287     return true;
288   }
289   struct stat sb{};
290   if (fstat(GetFileDescriptor(zip_handle_.get()), &sb) < 0) {
291     // If fstat fails on the zip archive, return true so the zip archive the resource system does
292     // attempt to refresh the ApkAsset.
293     return true;
294   }
295   return last_mod_time_ == sb.st_mtime;
296 }
297 
DirectoryAssetsProvider(std::string && path,time_t last_mod_time)298 DirectoryAssetsProvider::DirectoryAssetsProvider(std::string&& path, time_t last_mod_time)
299     : dir_(std::move(path)), last_mod_time_(last_mod_time) {}
300 
Create(std::string path)301 std::unique_ptr<DirectoryAssetsProvider> DirectoryAssetsProvider::Create(std::string path) {
302   struct stat sb;
303   const int result = stat(path.c_str(), &sb);
304   if (result == -1) {
305     LOG(ERROR) << "Failed to find directory '" << path << "'.";
306     return nullptr;
307   }
308 
309   if (!S_ISDIR(sb.st_mode)) {
310     LOG(ERROR) << "Path '" << path << "' is not a directory.";
311     return nullptr;
312   }
313 
314   if (path.back() != OS_PATH_SEPARATOR) {
315     path += OS_PATH_SEPARATOR;
316   }
317 
318   const bool isReadonly = isReadonlyFilesystem(path.c_str());
319   return std::unique_ptr<DirectoryAssetsProvider>(
320       new DirectoryAssetsProvider(std::move(path), isReadonly ? -1 : sb.st_mtime));
321 }
322 
OpenInternal(const std::string & path,Asset::AccessMode,bool * file_exists) const323 std::unique_ptr<Asset> DirectoryAssetsProvider::OpenInternal(const std::string& path,
324                                                              Asset::AccessMode /* mode */,
325                                                              bool* file_exists) const {
326   const std::string resolved_path = dir_ + path;
327   if (file_exists != nullptr) {
328     struct stat sb{};
329     *file_exists = (stat(resolved_path.c_str(), &sb) != -1) && S_ISREG(sb.st_mode);
330   }
331 
332   return CreateAssetFromFile(resolved_path);
333 }
334 
ForEachFile(const std::string &,base::function_ref<void (StringPiece,FileType)>) const335 bool DirectoryAssetsProvider::ForEachFile(
336     const std::string& /* root_path */,
337     base::function_ref<void(StringPiece, FileType)> /* f */) const {
338   return true;
339 }
340 
GetPath() const341 std::optional<std::string_view> DirectoryAssetsProvider::GetPath() const {
342   return dir_;
343 }
344 
GetDebugName() const345 const std::string& DirectoryAssetsProvider::GetDebugName() const {
346   return dir_;
347 }
348 
IsUpToDate() const349 bool DirectoryAssetsProvider::IsUpToDate() const {
350   if (last_mod_time_ == -1) {
351     return true;
352   }
353   struct stat sb;
354   if (stat(dir_.c_str(), &sb) < 0) {
355     // If stat fails on the zip archive, return true so the zip archive the resource system does
356     // attempt to refresh the ApkAsset.
357     return true;
358   }
359   return last_mod_time_ == sb.st_mtime;
360 }
361 
MultiAssetsProvider(std::unique_ptr<AssetsProvider> && primary,std::unique_ptr<AssetsProvider> && secondary)362 MultiAssetsProvider::MultiAssetsProvider(std::unique_ptr<AssetsProvider>&& primary,
363                                          std::unique_ptr<AssetsProvider>&& secondary)
364     : primary_(std::move(primary)), secondary_(std::move(secondary)) {
365   debug_name_ = primary_->GetDebugName() + " and " + secondary_->GetDebugName();
366   path_ = (primary_->GetDebugName() != kEmptyDebugString) ? primary_->GetPath()
367                                                           : secondary_->GetPath();
368 }
369 
Create(std::unique_ptr<AssetsProvider> && primary,std::unique_ptr<AssetsProvider> && secondary)370 std::unique_ptr<AssetsProvider> MultiAssetsProvider::Create(
371     std::unique_ptr<AssetsProvider>&& primary, std::unique_ptr<AssetsProvider>&& secondary) {
372   if (primary == nullptr || secondary == nullptr) {
373     return nullptr;
374   }
375   return std::unique_ptr<MultiAssetsProvider>(new MultiAssetsProvider(std::move(primary),
376                                                                       std::move(secondary)));
377 }
378 
OpenInternal(const std::string & path,Asset::AccessMode mode,bool * file_exists) const379 std::unique_ptr<Asset> MultiAssetsProvider::OpenInternal(const std::string& path,
380                                                          Asset::AccessMode mode,
381                                                          bool* file_exists) const {
382   auto asset = primary_->Open(path, mode, file_exists);
383   return (asset) ? std::move(asset) : secondary_->Open(path, mode, file_exists);
384 }
385 
ForEachFile(const std::string & root_path,base::function_ref<void (StringPiece,FileType)> f) const386 bool MultiAssetsProvider::ForEachFile(
387     const std::string& root_path,
388     base::function_ref<void(StringPiece, FileType)> f) const {
389   return primary_->ForEachFile(root_path, f) && secondary_->ForEachFile(root_path, f);
390 }
391 
GetPath() const392 std::optional<std::string_view> MultiAssetsProvider::GetPath() const {
393   return path_;
394 }
395 
GetDebugName() const396 const std::string& MultiAssetsProvider::GetDebugName() const {
397   return debug_name_;
398 }
399 
IsUpToDate() const400 bool MultiAssetsProvider::IsUpToDate() const {
401   return primary_->IsUpToDate() && secondary_->IsUpToDate();
402 }
403 
EmptyAssetsProvider(std::optional<std::string> && path)404 EmptyAssetsProvider::EmptyAssetsProvider(std::optional<std::string>&& path) :
405     path_(std::move(path)) {}
406 
Create()407 std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create() {
408   return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider({}));
409 }
410 
Create(std::string path)411 std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create(std::string path) {
412   return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider(std::move(path)));
413 }
414 
OpenInternal(const std::string &,Asset::AccessMode,bool * file_exists) const415 std::unique_ptr<Asset> EmptyAssetsProvider::OpenInternal(const std::string& /* path */,
416                                                          Asset::AccessMode /* mode */,
417                                                          bool* file_exists) const {
418   if (file_exists) {
419     *file_exists = false;
420   }
421   return nullptr;
422 }
423 
ForEachFile(const std::string &,base::function_ref<void (StringPiece,FileType)>) const424 bool EmptyAssetsProvider::ForEachFile(
425     const std::string& /* root_path */,
426     base::function_ref<void(StringPiece, FileType)> /* f */) const {
427   return true;
428 }
429 
GetPath() const430 std::optional<std::string_view> EmptyAssetsProvider::GetPath() const {
431   if (path_.has_value()) {
432     return *path_;
433   }
434   return {};
435 }
436 
GetDebugName() const437 const std::string& EmptyAssetsProvider::GetDebugName() const {
438   if (path_.has_value()) {
439     return *path_;
440   }
441   const static std::string kEmpty = kEmptyDebugString;
442   return kEmpty;
443 }
444 
IsUpToDate() const445 bool EmptyAssetsProvider::IsUpToDate() const {
446   return true;
447 }
448 
449 }  // namespace android
450