blob: a6aae6fe11688f712e6a367f20324106fa75f856 [file] [log] [blame]
Than McIntoshf831e6d2016-02-01 19:50:20 -05001/*
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 Cui8f680f62016-03-18 18:47:43 -070027#include <memory>
28
Than McIntoshf831e6d2016-02-01 19:50:20 -050029#include <android-base/file.h>
30#include <android-base/logging.h>
Yabin Cui2a53ff32018-05-21 17:37:00 -070031#include <android-base/strings.h>
Yabin Cui569f64a2016-02-05 17:32:08 -080032#include <ziparchive/zip_archive.h>
Than McIntoshf831e6d2016-02-01 19:50:20 -050033#include "read_elf.h"
34#include "utils.h"
Than McIntoshf831e6d2016-02-01 19:50:20 -050035
Yabin Cui8422f342018-05-09 17:27:27 -070036std::unordered_map<std::string, ApkInspector::ApkNode> ApkInspector::embedded_elf_cache_;
Than McIntoshf831e6d2016-02-01 19:50:20 -050037
Yabin Cui41e32ca2016-02-18 12:11:40 -080038EmbeddedElf* ApkInspector::FindElfInApkByOffset(const std::string& apk_path, uint64_t file_offset) {
Than McIntoshf831e6d2016-02-01 19:50:20 -050039 // Already in cache?
Yabin Cui8422f342018-05-09 17:27:27 -070040 ApkNode& node = embedded_elf_cache_[apk_path];
41 auto it = node.offset_map.find(file_offset);
42 if (it != node.offset_map.end()) {
Yabin Cuib1a885b2016-02-14 19:18:02 -080043 return it->second.get();
Than McIntoshf831e6d2016-02-01 19:50:20 -050044 }
Yabin Cuib1a885b2016-02-14 19:18:02 -080045 std::unique_ptr<EmbeddedElf> elf = FindElfInApkByOffsetWithoutCache(apk_path, file_offset);
46 EmbeddedElf* result = elf.get();
Yabin Cui8422f342018-05-09 17:27:27 -070047 node.offset_map[file_offset] = std::move(elf);
48 if (result != nullptr) {
49 node.name_map[result->entry_name()] = result;
50 }
Yabin Cuib1a885b2016-02-14 19:18:02 -080051 return result;
52}
Than McIntoshf831e6d2016-02-01 19:50:20 -050053
Yabin Cui8422f342018-05-09 17:27:27 -070054EmbeddedElf* ApkInspector::FindElfInApkByName(const std::string& apk_path,
55 const std::string& entry_name) {
56 ApkNode& node = embedded_elf_cache_[apk_path];
57 auto it = node.name_map.find(entry_name);
58 if (it != node.name_map.end()) {
59 return it->second;
60 }
61 std::unique_ptr<EmbeddedElf> elf = FindElfInApkByNameWithoutCache(apk_path, entry_name);
62 EmbeddedElf* result = elf.get();
63 node.name_map[entry_name] = result;
64 if (result != nullptr) {
65 node.offset_map[result->entry_offset()] = std::move(elf);
66 }
67 return result;
68}
69
70std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByOffsetWithoutCache(
71 const std::string& apk_path, uint64_t file_offset) {
Yabin Cui2a53ff32018-05-21 17:37:00 -070072 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(apk_path);
Yabin Cuibe7ec662016-03-02 13:56:28 -080073 if (!ahelper) {
Than McIntoshf831e6d2016-02-01 19:50:20 -050074 return nullptr;
75 }
Than McIntoshf831e6d2016-02-01 19:50:20 -050076
77 // Iterate through the zip file. Look for a zip entry corresponding
78 // to an uncompressed blob whose range intersects with the mmap
79 // offset we're interested in.
Than McIntoshf831e6d2016-02-01 19:50:20 -050080 bool found = false;
Yabin Cui2a53ff32018-05-21 17:37:00 -070081 ZipEntry found_entry;
82 std::string found_entry_name;
83 bool result = ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) {
84 if (entry.method == kCompressStored &&
85 file_offset >= static_cast<uint64_t>(entry.offset) &&
86 file_offset < static_cast<uint64_t>(entry.offset) + entry.uncompressed_length) {
Than McIntoshf831e6d2016-02-01 19:50:20 -050087 found = true;
Yabin Cui2a53ff32018-05-21 17:37:00 -070088 found_entry = entry;
89 found_entry_name = name;
90 return false;
Than McIntoshf831e6d2016-02-01 19:50:20 -050091 }
Yabin Cui2a53ff32018-05-21 17:37:00 -070092 return true;
93 });
94 if (!result || !found) {
Than McIntoshf831e6d2016-02-01 19:50:20 -050095 return nullptr;
96 }
97
98 // We found something in the zip file at the right spot. Is it an ELF?
Yabin Cui2a53ff32018-05-21 17:37:00 -070099 if (lseek(ahelper->GetFd(), found_entry.offset, SEEK_SET) != found_entry.offset) {
100 PLOG(ERROR) << "lseek() failed in " << apk_path << " offset " << found_entry.offset;
Than McIntoshf831e6d2016-02-01 19:50:20 -0500101 return nullptr;
102 }
Yabin Cui2a53ff32018-05-21 17:37:00 -0700103 if (IsValidElfFile(ahelper->GetFd()) != ElfStatus::NO_ERROR) {
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700104 // Omit files that are not ELF files.
Than McIntoshf831e6d2016-02-01 19:50:20 -0500105 return nullptr;
106 }
Yabin Cui2a53ff32018-05-21 17:37:00 -0700107 return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, found_entry_name,
108 found_entry.offset,
109 found_entry.uncompressed_length));
Than McIntoshf831e6d2016-02-01 19:50:20 -0500110}
111
Yabin Cui8422f342018-05-09 17:27:27 -0700112std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByNameWithoutCache(
113 const std::string& apk_path, const std::string& entry_name) {
Yabin Cui2a53ff32018-05-21 17:37:00 -0700114 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(apk_path);
Yabin Cuibe7ec662016-03-02 13:56:28 -0800115 if (!ahelper) {
Yabin Cui8422f342018-05-09 17:27:27 -0700116 return nullptr;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800117 }
Yabin Cuib1a885b2016-02-14 19:18:02 -0800118 ZipEntry zentry;
Yabin Cui2a53ff32018-05-21 17:37:00 -0700119 if (!ahelper->FindEntry(entry_name, &zentry)) {
Yabin Cuib1a885b2016-02-14 19:18:02 -0800120 return nullptr;
121 }
Yabin Cui8422f342018-05-09 17:27:27 -0700122 if (zentry.method != kCompressStored || zentry.compressed_length != zentry.uncompressed_length) {
Yabin Cui8422f342018-05-09 17:27:27 -0700123 return nullptr;
124 }
125 return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, entry_name, zentry.offset,
126 zentry.uncompressed_length));
Than McIntoshf831e6d2016-02-01 19:50:20 -0500127}
128
Yabin Cuib1a885b2016-02-14 19:18:02 -0800129// Refer file in apk in compliance with http://developer.android.com/reference/java/net/JarURLConnection.html.
130std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename) {
131 return apk_path + "!/" + elf_filename;
132}
133
134std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path) {
135 size_t pos = path.find("!/");
136 if (pos == std::string::npos) {
137 return std::make_tuple(false, "", "");
138 }
139 return std::make_tuple(true, path.substr(0, pos), path.substr(pos + 2));
140}
Yabin Cui2a53ff32018-05-21 17:37:00 -0700141
Joel Fernandes7e8b2b92018-08-24 12:20:28 -0700142// Parse path like "[anon:dalvik-classes.dex extracted in memory from /..base.apk] (deleted)",
143// or "/dev/ashmem/dalvik-classes.dex extracted in memory from /..base.apk (deleted)" on Android P.
Yabin Cui2a53ff32018-05-21 17:37:00 -0700144bool ParseExtractedInMemoryPath(const std::string& path, std::string* zip_path,
145 std::string* entry_name) {
Joel Fernandes7e8b2b92018-08-24 12:20:28 -0700146 const char* prefixes[2] = {"[anon:dalvik-", "/dev/ashmem/dalvik-"};
Yabin Cui2a53ff32018-05-21 17:37:00 -0700147 const char* key = " extracted in memory from ";
148 size_t pos = path.find(key);
Joel Fernandes7e8b2b92018-08-24 12:20:28 -0700149 if (pos != std::string::npos) {
150 for (const char* prefix : prefixes) {
151 if (android::base::StartsWith(path, prefix)) {
152 size_t entry_name_start = strlen(prefix);
153 size_t entry_name_end = pos;
154 size_t zip_path_start = pos + strlen(key);
155 size_t zip_path_end = path.find_first_of(" ]", zip_path_start);
156 if (zip_path_end == std::string::npos) {
157 zip_path_end = path.size();
158 }
159 if (entry_name_start < entry_name_end && zip_path_start < zip_path_end) {
160 *entry_name = path.substr(entry_name_start, entry_name_end - entry_name_start);
161 *zip_path = path.substr(zip_path_start, zip_path_end - zip_path_start);
Yabin Cui16ab77d2018-12-13 11:34:17 -0800162 size_t multidex_separator_pos = zip_path->find('!');
163 if (multidex_separator_pos != std::string::npos) {
164 zip_path->resize(multidex_separator_pos);
165 }
Joel Fernandes7e8b2b92018-08-24 12:20:28 -0700166 return true;
167 }
168 }
Yabin Cui2a53ff32018-05-21 17:37:00 -0700169 }
170 }
171 return false;
172}