read_apk.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. **
  3. ** Copyright 2016, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include "read_apk.h"
  18. #include <errno.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. #include <memory>
  25. #include <android-base/file.h>
  26. #include <android-base/logging.h>
  27. #include <android-base/strings.h>
  28. #include <ziparchive/zip_archive.h>
  29. #include "read_elf.h"
  30. #include "utils.h"
  31. std::unordered_map<std::string, ApkInspector::ApkNode> ApkInspector::embedded_elf_cache_;
  32. EmbeddedElf* ApkInspector::FindElfInApkByOffset(const std::string& apk_path, uint64_t file_offset) {
  33. // Already in cache?
  34. ApkNode& node = embedded_elf_cache_[apk_path];
  35. auto it = node.offset_map.find(file_offset);
  36. if (it != node.offset_map.end()) {
  37. return it->second.get();
  38. }
  39. std::unique_ptr<EmbeddedElf> elf = FindElfInApkByOffsetWithoutCache(apk_path, file_offset);
  40. EmbeddedElf* result = elf.get();
  41. node.offset_map[file_offset] = std::move(elf);
  42. if (result != nullptr) {
  43. node.name_map[result->entry_name()] = result;
  44. }
  45. return result;
  46. }
  47. EmbeddedElf* ApkInspector::FindElfInApkByName(const std::string& apk_path,
  48. const std::string& entry_name) {
  49. ApkNode& node = embedded_elf_cache_[apk_path];
  50. auto it = node.name_map.find(entry_name);
  51. if (it != node.name_map.end()) {
  52. return it->second;
  53. }
  54. std::unique_ptr<EmbeddedElf> elf = FindElfInApkByNameWithoutCache(apk_path, entry_name);
  55. EmbeddedElf* result = elf.get();
  56. node.name_map[entry_name] = result;
  57. if (result != nullptr) {
  58. node.offset_map[result->entry_offset()] = std::move(elf);
  59. }
  60. return result;
  61. }
  62. std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByOffsetWithoutCache(
  63. const std::string& apk_path, uint64_t file_offset) {
  64. std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(apk_path);
  65. if (!ahelper) {
  66. return nullptr;
  67. }
  68. // Iterate through the zip file. Look for a zip entry corresponding
  69. // to an uncompressed blob whose range intersects with the mmap
  70. // offset we're interested in.
  71. bool found = false;
  72. ZipEntry found_entry;
  73. std::string found_entry_name;
  74. bool result = ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) {
  75. if (entry.method == kCompressStored &&
  76. file_offset >= static_cast<uint64_t>(entry.offset) &&
  77. file_offset < static_cast<uint64_t>(entry.offset) + entry.uncompressed_length) {
  78. found = true;
  79. found_entry = entry;
  80. found_entry_name = name;
  81. return false;
  82. }
  83. return true;
  84. });
  85. if (!result || !found) {
  86. return nullptr;
  87. }
  88. // We found something in the zip file at the right spot. Is it an ELF?
  89. if (lseek(ahelper->GetFd(), found_entry.offset, SEEK_SET) != found_entry.offset) {
  90. PLOG(ERROR) << "lseek() failed in " << apk_path << " offset " << found_entry.offset;
  91. return nullptr;
  92. }
  93. if (IsValidElfFile(ahelper->GetFd()) != ElfStatus::NO_ERROR) {
  94. // Omit files that are not ELF files.
  95. return nullptr;
  96. }
  97. return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, found_entry_name,
  98. found_entry.offset,
  99. found_entry.uncompressed_length));
  100. }
  101. std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByNameWithoutCache(
  102. const std::string& apk_path, const std::string& entry_name) {
  103. std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(apk_path);
  104. if (!ahelper) {
  105. return nullptr;
  106. }
  107. ZipEntry zentry;
  108. if (!ahelper->FindEntry(entry_name, &zentry)) {
  109. return nullptr;
  110. }
  111. if (zentry.method != kCompressStored || zentry.compressed_length != zentry.uncompressed_length) {
  112. return nullptr;
  113. }
  114. return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, entry_name, zentry.offset,
  115. zentry.uncompressed_length));
  116. }
  117. // Refer file in apk in compliance with http://developer.android.com/reference/java/net/JarURLConnection.html.
  118. std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename) {
  119. return apk_path + "!/" + elf_filename;
  120. }
  121. std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path) {
  122. size_t pos = path.find("!/");
  123. if (pos == std::string::npos) {
  124. return std::make_tuple(false, "", "");
  125. }
  126. return std::make_tuple(true, path.substr(0, pos), path.substr(pos + 2));
  127. }
  128. // Parse path like "[anon:dalvik-classes.dex extracted in memory from /..base.apk] (deleted)",
  129. // or "/dev/ashmem/dalvik-classes.dex extracted in memory from /..base.apk (deleted)" on Android P.
  130. bool ParseExtractedInMemoryPath(const std::string& path, std::string* zip_path,
  131. std::string* entry_name) {
  132. const char* prefixes[2] = {"[anon:dalvik-", "/dev/ashmem/dalvik-"};
  133. const char* key = " extracted in memory from ";
  134. size_t pos = path.find(key);
  135. if (pos != std::string::npos) {
  136. for (const char* prefix : prefixes) {
  137. if (android::base::StartsWith(path, prefix)) {
  138. size_t entry_name_start = strlen(prefix);
  139. size_t entry_name_end = pos;
  140. size_t zip_path_start = pos + strlen(key);
  141. size_t zip_path_end = path.find_first_of(" ]", zip_path_start);
  142. if (zip_path_end == std::string::npos) {
  143. zip_path_end = path.size();
  144. }
  145. if (entry_name_start < entry_name_end && zip_path_start < zip_path_end) {
  146. *entry_name = path.substr(entry_name_start, entry_name_end - entry_name_start);
  147. *zip_path = path.substr(zip_path_start, zip_path_end - zip_path_start);
  148. size_t multidex_separator_pos = zip_path->find('!');
  149. if (multidex_separator_pos != std::string::npos) {
  150. zip_path->resize(multidex_separator_pos);
  151. }
  152. return true;
  153. }
  154. }
  155. }
  156. }
  157. return false;
  158. }