Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 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 | |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 27 | #include <memory> |
| 28 | |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 31 | #include <ziparchive/zip_archive.h> |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 32 | #include "read_elf.h" |
| 33 | #include "utils.h" |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 34 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 35 | std::map<ApkInspector::ApkOffset, std::unique_ptr<EmbeddedElf>> ApkInspector::embedded_elf_cache_; |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 36 | |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 37 | EmbeddedElf* ApkInspector::FindElfInApkByOffset(const std::string& apk_path, uint64_t file_offset) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 38 | // Already in cache? |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 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(); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 43 | } |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 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 | } |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 49 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 50 | std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByOffsetWithoutCache(const std::string& apk_path, |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 51 | uint64_t file_offset) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 52 | // Crack open the apk(zip) file and take a look. |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 53 | if (!IsValidApkPath(apk_path)) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 54 | return nullptr; |
| 55 | } |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 56 | |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 57 | FileHelper fhelper = FileHelper::OpenReadOnly(apk_path); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 58 | if (!fhelper) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 59 | return nullptr; |
| 60 | } |
| 61 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 62 | ArchiveHelper ahelper(fhelper.fd(), apk_path); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 63 | if (!ahelper) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 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; |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 79 | while ((zrc = Next(iteration_cookie, &zentry, &zname)) == 0) { |
| 80 | if (zentry.method == kCompressStored && |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 81 | file_offset >= static_cast<uint64_t>(zentry.offset) && |
| 82 | file_offset < static_cast<uint64_t>(zentry.offset + zentry.uncompressed_length)) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 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); |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 101 | ElfStatus result = IsValidElfFile(fhelper.fd()); |
| 102 | if (result != ElfStatus::NO_ERROR) { |
| 103 | LOG(ERROR) << "problems reading ELF from " << apk_path << " entry '" |
| 104 | << entry_name << "': " << result; |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 105 | return nullptr; |
| 106 | } |
| 107 | |
| 108 | // Elf found: add EmbeddedElf to vector, update cache. |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 109 | return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, entry_name, zentry.offset, |
| 110 | zentry.uncompressed_length)); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 113 | std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByName(const std::string& apk_path, |
| 114 | const std::string& elf_filename) { |
| 115 | if (!IsValidApkPath(apk_path)) { |
| 116 | return nullptr; |
| 117 | } |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 118 | FileHelper fhelper = FileHelper::OpenReadOnly(apk_path); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 119 | if (!fhelper) { |
| 120 | return nullptr; |
| 121 | } |
| 122 | ArchiveHelper ahelper(fhelper.fd(), apk_path); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 123 | if (!ahelper) { |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 124 | return nullptr; |
| 125 | } |
| 126 | ZipArchiveHandle& handle = ahelper.archive_handle(); |
| 127 | ZipEntry zentry; |
| 128 | int32_t rc = FindEntry(handle, ZipString(elf_filename.c_str()), &zentry); |
| 129 | if (rc != 0) { |
| 130 | LOG(ERROR) << "failed to find " << elf_filename << " in " << apk_path |
| 131 | << ": " << ErrorCodeString(rc); |
| 132 | return nullptr; |
| 133 | } |
| 134 | if (zentry.method != kCompressStored || zentry.compressed_length != zentry.uncompressed_length) { |
| 135 | LOG(ERROR) << "shared library " << elf_filename << " in " << apk_path << " is compressed"; |
| 136 | return nullptr; |
| 137 | } |
| 138 | return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, elf_filename, zentry.offset, |
| 139 | zentry.uncompressed_length)); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 142 | bool IsValidApkPath(const std::string& apk_path) { |
| 143 | static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04 }; |
| 144 | if (!IsRegularFile(apk_path)) { |
| 145 | return false; |
| 146 | } |
| 147 | std::string mode = std::string("rb") + CLOSE_ON_EXEC_MODE; |
| 148 | FILE* fp = fopen(apk_path.c_str(), mode.c_str()); |
| 149 | if (fp == nullptr) { |
| 150 | return false; |
| 151 | } |
| 152 | char buf[4]; |
| 153 | if (fread(buf, 4, 1, fp) != 1) { |
| 154 | fclose(fp); |
| 155 | return false; |
| 156 | } |
| 157 | fclose(fp); |
| 158 | return memcmp(buf, zip_preamble, 4) == 0; |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 161 | // Refer file in apk in compliance with http://developer.android.com/reference/java/net/JarURLConnection.html. |
| 162 | std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename) { |
| 163 | return apk_path + "!/" + elf_filename; |
| 164 | } |
| 165 | |
| 166 | std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path) { |
| 167 | size_t pos = path.find("!/"); |
| 168 | if (pos == std::string::npos) { |
| 169 | return std::make_tuple(false, "", ""); |
| 170 | } |
| 171 | return std::make_tuple(true, path.substr(0, pos), path.substr(pos + 2)); |
| 172 | } |
| 173 | |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 174 | ElfStatus GetBuildIdFromApkFile(const std::string& apk_path, const std::string& elf_filename, |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 175 | BuildId* build_id) { |
| 176 | std::unique_ptr<EmbeddedElf> ee = ApkInspector::FindElfInApkByName(apk_path, elf_filename); |
| 177 | if (ee == nullptr) { |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 178 | return ElfStatus::FILE_NOT_FOUND; |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 179 | } |
| 180 | return GetBuildIdFromEmbeddedElfFile(apk_path, ee->entry_offset(), ee->entry_size(), build_id); |
| 181 | } |
| 182 | |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 183 | ElfStatus ParseSymbolsFromApkFile(const std::string& apk_path, const std::string& elf_filename, |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 184 | const BuildId& expected_build_id, |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 185 | const std::function<void(const ElfFileSymbol&)>& callback) { |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 186 | std::unique_ptr<EmbeddedElf> ee = ApkInspector::FindElfInApkByName(apk_path, elf_filename); |
| 187 | if (ee == nullptr) { |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 188 | return ElfStatus::FILE_NOT_FOUND; |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 189 | } |
| 190 | return ParseSymbolsFromEmbeddedElfFile(apk_path, ee->entry_offset(), ee->entry_size(), |
| 191 | expected_build_id, callback); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 192 | } |