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 18 #include "read_apk.h" 19 20 #include <errno.h> 21 #include <stdio.h> 22 #include <string.h> 23 #include <sys/stat.h> 24 #include <sys/types.h> 25 #include <unistd.h> 26 27 #include <memory> 28 29 #include <android-base/file.h> 30 #include <android-base/logging.h> 31 #include <ziparchive/zip_archive.h> 32 #include "read_elf.h" 33 #include "utils.h" 34 35 std::map<ApkInspector::ApkOffset, std::unique_ptr<EmbeddedElf>> ApkInspector::embedded_elf_cache_; 36 FindElfInApkByOffset(const std::string & apk_path,uint64_t file_offset)37 EmbeddedElf* ApkInspector::FindElfInApkByOffset(const std::string& apk_path, uint64_t file_offset) { 38 // Already in cache? 39 ApkOffset ami(apk_path, file_offset); 40 auto it = embedded_elf_cache_.find(ami); 41 if (it != embedded_elf_cache_.end()) { 42 return it->second.get(); 43 } 44 std::unique_ptr<EmbeddedElf> elf = FindElfInApkByOffsetWithoutCache(apk_path, file_offset); 45 EmbeddedElf* result = elf.get(); 46 embedded_elf_cache_[ami] = std::move(elf); 47 return result; 48 } 49 FindElfInApkByOffsetWithoutCache(const std::string & apk_path,uint64_t file_offset)50 std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByOffsetWithoutCache(const std::string& apk_path, 51 uint64_t file_offset) { 52 // Crack open the apk(zip) file and take a look. 53 if (!IsValidApkPath(apk_path)) { 54 return nullptr; 55 } 56 57 FileHelper fhelper = FileHelper::OpenReadOnly(apk_path); 58 if (!fhelper) { 59 return nullptr; 60 } 61 62 ArchiveHelper ahelper(fhelper.fd(), apk_path); 63 if (!ahelper) { 64 return nullptr; 65 } 66 ZipArchiveHandle &handle = ahelper.archive_handle(); 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 void* iteration_cookie; 72 if (StartIteration(handle, &iteration_cookie, nullptr, nullptr) < 0) { 73 return nullptr; 74 } 75 ZipEntry zentry; 76 ZipString zname; 77 bool found = false; 78 int zrc; 79 while ((zrc = Next(iteration_cookie, &zentry, &zname)) == 0) { 80 if (zentry.method == kCompressStored && 81 file_offset >= static_cast<uint64_t>(zentry.offset) && 82 file_offset < static_cast<uint64_t>(zentry.offset + zentry.uncompressed_length)) { 83 // Found. 84 found = true; 85 break; 86 } 87 } 88 EndIteration(iteration_cookie); 89 if (!found) { 90 return nullptr; 91 } 92 93 // We found something in the zip file at the right spot. Is it an ELF? 94 if (lseek(fhelper.fd(), zentry.offset, SEEK_SET) != zentry.offset) { 95 PLOG(ERROR) << "lseek() failed in " << apk_path << " offset " << zentry.offset; 96 return nullptr; 97 } 98 std::string entry_name; 99 entry_name.resize(zname.name_length,'\0'); 100 memcpy(&entry_name[0], zname.name, zname.name_length); 101 if (!IsValidElfFile(fhelper.fd())) { 102 LOG(ERROR) << "problems reading ELF from in " << apk_path << " entry '" 103 << entry_name << "'"; 104 return nullptr; 105 } 106 107 // Elf found: add EmbeddedElf to vector, update cache. 108 return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, entry_name, zentry.offset, 109 zentry.uncompressed_length)); 110 } 111 FindElfInApkByName(const std::string & apk_path,const std::string & elf_filename)112 std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByName(const std::string& apk_path, 113 const std::string& elf_filename) { 114 if (!IsValidApkPath(apk_path)) { 115 return nullptr; 116 } 117 FileHelper fhelper = FileHelper::OpenReadOnly(apk_path); 118 if (!fhelper) { 119 return nullptr; 120 } 121 ArchiveHelper ahelper(fhelper.fd(), apk_path); 122 if (!ahelper) { 123 return nullptr; 124 } 125 ZipArchiveHandle& handle = ahelper.archive_handle(); 126 ZipEntry zentry; 127 int32_t rc = FindEntry(handle, ZipString(elf_filename.c_str()), &zentry); 128 if (rc != 0) { 129 LOG(ERROR) << "failed to find " << elf_filename << " in " << apk_path 130 << ": " << ErrorCodeString(rc); 131 return nullptr; 132 } 133 if (zentry.method != kCompressStored || zentry.compressed_length != zentry.uncompressed_length) { 134 LOG(ERROR) << "shared library " << elf_filename << " in " << apk_path << " is compressed"; 135 return nullptr; 136 } 137 return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, elf_filename, zentry.offset, 138 zentry.uncompressed_length)); 139 } 140 IsValidApkPath(const std::string & apk_path)141 bool IsValidApkPath(const std::string& apk_path) { 142 static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04 }; 143 if (!IsRegularFile(apk_path)) { 144 return false; 145 } 146 std::string mode = std::string("rb") + CLOSE_ON_EXEC_MODE; 147 FILE* fp = fopen(apk_path.c_str(), mode.c_str()); 148 if (fp == nullptr) { 149 return false; 150 } 151 char buf[4]; 152 if (fread(buf, 4, 1, fp) != 1) { 153 fclose(fp); 154 return false; 155 } 156 fclose(fp); 157 return memcmp(buf, zip_preamble, 4) == 0; 158 } 159 160 // Refer file in apk in compliance with http://developer.android.com/reference/java/net/JarURLConnection.html. GetUrlInApk(const std::string & apk_path,const std::string & elf_filename)161 std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename) { 162 return apk_path + "!/" + elf_filename; 163 } 164 SplitUrlInApk(const std::string & path)165 std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path) { 166 size_t pos = path.find("!/"); 167 if (pos == std::string::npos) { 168 return std::make_tuple(false, "", ""); 169 } 170 return std::make_tuple(true, path.substr(0, pos), path.substr(pos + 2)); 171 } 172 GetBuildIdFromApkFile(const std::string & apk_path,const std::string & elf_filename,BuildId * build_id)173 bool GetBuildIdFromApkFile(const std::string& apk_path, const std::string& elf_filename, 174 BuildId* build_id) { 175 std::unique_ptr<EmbeddedElf> ee = ApkInspector::FindElfInApkByName(apk_path, elf_filename); 176 if (ee == nullptr) { 177 return false; 178 } 179 return GetBuildIdFromEmbeddedElfFile(apk_path, ee->entry_offset(), ee->entry_size(), build_id); 180 } 181 ParseSymbolsFromApkFile(const std::string & apk_path,const std::string & elf_filename,const BuildId & expected_build_id,std::function<void (const ElfFileSymbol &)> callback)182 bool ParseSymbolsFromApkFile(const std::string& apk_path, const std::string& elf_filename, 183 const BuildId& expected_build_id, 184 std::function<void(const ElfFileSymbol&)> callback) { 185 std::unique_ptr<EmbeddedElf> ee = ApkInspector::FindElfInApkByName(apk_path, elf_filename); 186 if (ee == nullptr) { 187 return false; 188 } 189 return ParseSymbolsFromEmbeddedElfFile(apk_path, ee->entry_offset(), ee->entry_size(), 190 expected_build_id, callback); 191 } 192