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