blob: 389ade59200ce273a4112bc409ad3f0c79588336 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/Idmap.h"
18
Mårten Kongstad02751232018-04-27 13:16:32 +020019#include <algorithm>
20#include <iostream>
21#include <iterator>
22#include <limits>
23#include <map>
24#include <memory>
25#include <set>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include "android-base/macros.h"
31#include "android-base/stringprintf.h"
32#include "androidfw/AssetManager2.h"
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070033#include "idmap2/ResourceMapping.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020034#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010035#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010036#include "idmap2/SysTrace.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020037#include "idmap2/ZipFile.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070038#include "utils/String16.h"
39#include "utils/String8.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020040
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010041namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020042
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010043namespace {
44
Mårten Kongstadcf281362018-11-28 19:32:25 +010045class MatchingResources {
46 public:
Mårten Kongstad02751232018-04-27 13:16:32 +020047 void Add(ResourceId target_resid, ResourceId overlay_resid) {
48 TypeId target_typeid = EXTRACT_TYPE(target_resid);
Mårten Kongstadcf281362018-11-28 19:32:25 +010049 if (map_.find(target_typeid) == map_.end()) {
50 map_.emplace(target_typeid, std::set<std::pair<ResourceId, ResourceId>>());
Mårten Kongstad02751232018-04-27 13:16:32 +020051 }
Mårten Kongstadcf281362018-11-28 19:32:25 +010052 map_[target_typeid].insert(std::make_pair(target_resid, overlay_resid));
Mårten Kongstad02751232018-04-27 13:16:32 +020053 }
54
Yi Kong974d5162019-03-06 16:18:11 -080055 inline const std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>>& WARN_UNUSED
Mårten Kongstadcf622492019-03-19 22:40:58 +010056 Map() const {
Mårten Kongstadcf281362018-11-28 19:32:25 +010057 return map_;
58 }
59
60 private:
Mårten Kongstad02751232018-04-27 13:16:32 +020061 // target type id -> set { pair { overlay entry id, overlay entry id } }
Mårten Kongstadcf281362018-11-28 19:32:25 +010062 std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>> map_;
Mårten Kongstad02751232018-04-27 13:16:32 +020063};
64
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010065bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) {
Mårten Kongstad02751232018-04-27 13:16:32 +020066 uint16_t value;
67 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) {
68 *out = dtohl(value);
69 return true;
70 }
71 return false;
72}
73
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010074bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
Mårten Kongstad02751232018-04-27 13:16:32 +020075 uint32_t value;
76 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
77 *out = dtohl(value);
78 return true;
79 }
80 return false;
81}
82
83// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010084bool WARN_UNUSED ReadString(std::istream& stream, char out[kIdmapStringLength]) {
Mårten Kongstad02751232018-04-27 13:16:32 +020085 char buf[kIdmapStringLength];
86 memset(buf, 0, sizeof(buf));
87 if (!stream.read(buf, sizeof(buf))) {
88 return false;
89 }
90 if (buf[sizeof(buf) - 1] != '\0') {
91 return false;
92 }
93 memcpy(out, buf, sizeof(buf));
94 return true;
95}
96
Mårten Kongstad9371dc12019-01-22 14:35:12 +010097Result<uint32_t> GetCrc(const ZipFile& zip) {
98 const Result<uint32_t> a = zip.Crc("resources.arsc");
99 const Result<uint32_t> b = zip.Crc("AndroidManifest.xml");
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100100 return a && b
101 ? Result<uint32_t>(*a ^ *b)
Mårten Kongstadce424902019-03-01 08:35:37 +0100102 : Error("failed to get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100103}
104
Mårten Kongstad744ccfe2018-12-20 14:56:14 +0100105} // namespace
106
Mårten Kongstad02751232018-04-27 13:16:32 +0200107std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
108 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
109
110 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
111 !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
112 !ReadString(stream, idmap_header->target_path_) ||
113 !ReadString(stream, idmap_header->overlay_path_)) {
114 return nullptr;
115 }
116
117 return std::move(idmap_header);
118}
119
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100120Result<Unit> IdmapHeader::IsUpToDate() const {
Mårten Kongstad02751232018-04-27 13:16:32 +0200121 if (magic_ != kIdmapMagic) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100122 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
Mårten Kongstad02751232018-04-27 13:16:32 +0200123 }
124
125 if (version_ != kIdmapCurrentVersion) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100126 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
Mårten Kongstad02751232018-04-27 13:16:32 +0200127 }
128
129 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_);
130 if (!target_zip) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100131 return Error("failed to open target %s", GetTargetPath().to_string().c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200132 }
133
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100134 Result<uint32_t> target_crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100135 if (!target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100136 return Error("failed to get target crc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200137 }
138
Mårten Kongstad0f763112018-11-19 14:14:37 +0100139 if (target_crc_ != *target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100140 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
141 *target_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200142 }
143
144 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_);
145 if (!overlay_zip) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100146 return Error("failed to open overlay %s", GetOverlayPath().to_string().c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200147 }
148
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100149 Result<uint32_t> overlay_crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100150 if (!overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100151 return Error("failed to get overlay crc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200152 }
153
Mårten Kongstad0f763112018-11-19 14:14:37 +0100154 if (overlay_crc_ != *overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100155 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
156 *overlay_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200157 }
158
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100159 return Unit{};
Mårten Kongstad02751232018-04-27 13:16:32 +0200160}
161
162std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
163 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
164
165 uint16_t target_package_id16;
166 if (!Read16(stream, &target_package_id16) || !Read16(stream, &idmap_data_header->type_count_)) {
167 return nullptr;
168 }
169 idmap_data_header->target_package_id_ = target_package_id16;
170
171 return std::move(idmap_data_header);
172}
173
174std::unique_ptr<const IdmapData::TypeEntry> IdmapData::TypeEntry::FromBinaryStream(
175 std::istream& stream) {
176 std::unique_ptr<IdmapData::TypeEntry> data(new IdmapData::TypeEntry());
Mårten Kongstadb8779022018-11-29 09:53:17 +0100177 uint16_t target_type16;
178 uint16_t overlay_type16;
179 uint16_t entry_count;
Mårten Kongstad02751232018-04-27 13:16:32 +0200180 if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) ||
181 !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) {
182 return nullptr;
183 }
184 data->target_type_id_ = target_type16;
185 data->overlay_type_id_ = overlay_type16;
186 for (uint16_t i = 0; i < entry_count; i++) {
187 ResourceId resid;
188 if (!Read32(stream, &resid)) {
189 return nullptr;
190 }
191 data->entries_.push_back(resid);
192 }
193
194 return std::move(data);
195}
196
197std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
198 std::unique_ptr<IdmapData> data(new IdmapData());
199 data->header_ = IdmapData::Header::FromBinaryStream(stream);
200 if (!data->header_) {
201 return nullptr;
202 }
203 for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) {
204 std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream);
205 if (!type) {
206 return nullptr;
207 }
208 data->type_entries_.push_back(std::move(type));
209 }
210 return std::move(data);
211}
212
213std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
214 const std::string& absolute_apk_path) {
215 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
216 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
217 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
218 replace(copy.begin(), copy.end(), '/', '@');
219 return absolute_dir + "/" + copy + "@idmap";
220}
221
Mårten Kongstadce424902019-03-01 08:35:37 +0100222Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100223 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200224 std::unique_ptr<Idmap> idmap(new Idmap());
225
226 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
227 if (!idmap->header_) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100228 return Error("failed to parse idmap header");
Mårten Kongstad02751232018-04-27 13:16:32 +0200229 }
230
231 // idmap version 0x01 does not specify the number of data blocks that follow
232 // the idmap header; assume exactly one data block
233 for (int i = 0; i < 1; i++) {
234 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
235 if (!data) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100236 return Error("failed to parse data block %d", i);
Mårten Kongstad02751232018-04-27 13:16:32 +0200237 }
238 idmap->data_.push_back(std::move(data));
239 }
240
Mårten Kongstadce424902019-03-01 08:35:37 +0100241 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200242}
243
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700244Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
245 const ResourceMapping& resource_mapping) {
246 if (resource_mapping.GetTargetToOverlayMap().empty()) {
247 return Error("no resources were overlaid");
248 }
249
250 MatchingResources matching_resources;
251 for (const auto mapping : resource_mapping.GetTargetToOverlayMap()) {
252 if (mapping.second.data_type != Res_value::TYPE_REFERENCE) {
253 // The idmap format must change to support non-references.
254 continue;
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800255 }
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700256
257 matching_resources.Add(mapping.first, mapping.second.data_value);
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800258 }
259
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700260 // encode idmap data
261 std::unique_ptr<IdmapData> data(new IdmapData());
262 const auto types_end = matching_resources.Map().cend();
263 for (auto ti = matching_resources.Map().cbegin(); ti != types_end; ++ti) {
264 auto ei = ti->second.cbegin();
265 std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry());
266 type->target_type_id_ = EXTRACT_TYPE(ei->first);
267 type->overlay_type_id_ = EXTRACT_TYPE(ei->second);
268 type->entry_offset_ = EXTRACT_ENTRY(ei->first);
269 EntryId last_target_entry = kNoEntry;
270 for (; ei != ti->second.cend(); ++ei) {
271 if (last_target_entry != kNoEntry) {
272 int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1;
273 type->entries_.insert(type->entries_.end(), count, kNoEntry);
274 }
275 type->entries_.push_back(EXTRACT_ENTRY(ei->second));
276 last_target_entry = EXTRACT_ENTRY(ei->first);
277 }
278 data->type_entries_.push_back(std::move(type));
279 }
280
281 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
282 data_header->target_package_id_ = resource_mapping.GetTargetPackageId();
283 data_header->type_count_ = data->type_entries_.size();
284 data->header_ = std::move(data_header);
285 return {std::move(data)};
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800286}
287
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700288Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& target_apk_assets,
Mårten Kongstadce424902019-03-01 08:35:37 +0100289 const ApkAssets& overlay_apk_assets,
290 const PolicyBitmask& fulfilled_policies,
291 bool enforce_overlayable) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100292 SYSTRACE << "Idmap::FromApkAssets";
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700293 const std::string& target_apk_path = target_apk_assets.GetPath();
294 const std::string& overlay_apk_path = overlay_apk_assets.GetPath();
Mårten Kongstad02751232018-04-27 13:16:32 +0200295
296 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
297 if (!target_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100298 return Error("failed to open target as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200299 }
300
301 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
302 if (!overlay_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100303 return Error("failed to open overlay as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200304 }
305
306 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
307 header->magic_ = kIdmapMagic;
308 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100309
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100310 Result<uint32_t> crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100311 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100312 return Error(crc.GetError(), "failed to get zip CRC for target");
Mårten Kongstad02751232018-04-27 13:16:32 +0200313 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100314 header->target_crc_ = *crc;
315
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100316 crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100317 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100318 return Error(crc.GetError(), "failed to get zip CRC for overlay");
Mårten Kongstad02751232018-04-27 13:16:32 +0200319 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100320 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200321
322 if (target_apk_path.size() > sizeof(header->target_path_)) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100323 return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
324 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200325 }
326 memset(header->target_path_, 0, sizeof(header->target_path_));
327 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
328
329 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700330 return Error("overlay apk path \"%s\" longer than maximum size %zu", overlay_apk_path.c_str(),
Mårten Kongstadce424902019-03-01 08:35:37 +0100331 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200332 }
333 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
334 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
335
336 std::unique_ptr<Idmap> idmap(new Idmap());
337 idmap->header_ = std::move(header);
338
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700339 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
340 if (!overlay_info) {
341 return overlay_info.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200342 }
343
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700344 auto resource_mapping =
345 ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *overlay_info,
346 fulfilled_policies, enforce_overlayable);
347 if (!resource_mapping) {
348 return resource_mapping.GetError();
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800349 }
350
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700351 auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping);
352 if (!idmap_data) {
353 return idmap_data.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200354 }
355
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700356 idmap->data_.push_back(std::move(*idmap_data));
Mårten Kongstadce424902019-03-01 08:35:37 +0100357 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200358}
359
360void IdmapHeader::accept(Visitor* v) const {
361 assert(v != nullptr);
362 v->visit(*this);
363}
364
365void IdmapData::Header::accept(Visitor* v) const {
366 assert(v != nullptr);
367 v->visit(*this);
368}
369
370void IdmapData::TypeEntry::accept(Visitor* v) const {
371 assert(v != nullptr);
372 v->visit(*this);
373}
374
375void IdmapData::accept(Visitor* v) const {
376 assert(v != nullptr);
377 v->visit(*this);
378 header_->accept(v);
379 auto end = type_entries_.cend();
380 for (auto iter = type_entries_.cbegin(); iter != end; ++iter) {
381 (*iter)->accept(v);
382 }
383}
384
385void Idmap::accept(Visitor* v) const {
386 assert(v != nullptr);
387 v->visit(*this);
388 header_->accept(v);
389 auto end = data_.cend();
390 for (auto iter = data_.cbegin(); iter != end; ++iter) {
391 (*iter)->accept(v);
392 }
393}
394
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100395} // namespace android::idmap2