blob: 706b842b3b4782c96b27cb40264cd1cae37668fd [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
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070045bool WARN_UNUSED Read8(std::istream& stream, uint8_t* out) {
46 uint8_t value;
47 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint8_t))) {
48 *out = value;
Mårten Kongstad02751232018-04-27 13:16:32 +020049 return true;
50 }
51 return false;
52}
53
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010054bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
Mårten Kongstad02751232018-04-27 13:16:32 +020055 uint32_t value;
56 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
57 *out = dtohl(value);
58 return true;
59 }
60 return false;
61}
62
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070063bool WARN_UNUSED ReadBuffer(std::istream& stream, std::unique_ptr<uint8_t[]>* out, size_t length) {
64 auto buffer = std::unique_ptr<uint8_t[]>(new uint8_t[length]);
65 if (stream.read(reinterpret_cast<char*>(buffer.get()), length)) {
66 *out = std::move(buffer);
67 return true;
68 }
69 return false;
70}
71
Mårten Kongstad02751232018-04-27 13:16:32 +020072// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020073bool WARN_UNUSED ReadString256(std::istream& stream, char out[kIdmapStringLength]) {
Mårten Kongstad02751232018-04-27 13:16:32 +020074 char buf[kIdmapStringLength];
75 memset(buf, 0, sizeof(buf));
76 if (!stream.read(buf, sizeof(buf))) {
77 return false;
78 }
79 if (buf[sizeof(buf) - 1] != '\0') {
80 return false;
81 }
82 memcpy(out, buf, sizeof(buf));
83 return true;
84}
85
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020086Result<std::string> ReadString(std::istream& stream) {
87 uint32_t size;
88 if (!Read32(stream, &size)) {
89 return Error("failed to read string size");
90 }
91 if (size == 0) {
92 return std::string("");
93 }
94 std::string buf(size, '\0');
95 if (!stream.read(buf.data(), size)) {
96 return Error("failed to read string of size %u", size);
97 }
98 // buf is guaranteed to be null terminated (with enough nulls to end on a word boundary)
99 buf.resize(strlen(buf.c_str()));
100 return buf;
101}
102
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700103} // namespace
104
105Result<uint32_t> GetPackageCrc(const ZipFile& zip) {
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100106 const Result<uint32_t> a = zip.Crc("resources.arsc");
107 const Result<uint32_t> b = zip.Crc("AndroidManifest.xml");
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100108 return a && b
109 ? Result<uint32_t>(*a ^ *b)
Mårten Kongstadce424902019-03-01 08:35:37 +0100110 : Error("failed to get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100111}
112
Mårten Kongstad02751232018-04-27 13:16:32 +0200113std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
114 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
115
116 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
117 !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200118 !ReadString256(stream, idmap_header->target_path_) ||
119 !ReadString256(stream, idmap_header->overlay_path_)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200120 return nullptr;
121 }
122
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200123 auto debug_str = ReadString(stream);
124 if (!debug_str) {
125 return nullptr;
126 }
127 idmap_header->debug_info_ = std::move(*debug_str);
128
Mårten Kongstad02751232018-04-27 13:16:32 +0200129 return std::move(idmap_header);
130}
131
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100132Result<Unit> IdmapHeader::IsUpToDate() const {
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700133 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_);
134 if (!target_zip) {
135 return Error("failed to open target %s", GetTargetPath().to_string().c_str());
136 }
137
138 Result<uint32_t> target_crc = GetPackageCrc(*target_zip);
139 if (!target_crc) {
140 return Error("failed to get target crc");
141 }
142
143 return IsUpToDate(*target_crc);
144}
145
146Result<Unit> IdmapHeader::IsUpToDate(uint32_t target_crc) const {
Mårten Kongstad02751232018-04-27 13:16:32 +0200147 if (magic_ != kIdmapMagic) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100148 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
Mårten Kongstad02751232018-04-27 13:16:32 +0200149 }
150
151 if (version_ != kIdmapCurrentVersion) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100152 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
Mårten Kongstad02751232018-04-27 13:16:32 +0200153 }
154
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700155 if (target_crc_ != target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100156 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700157 target_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200158 }
159
160 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_);
161 if (!overlay_zip) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100162 return Error("failed to open overlay %s", GetOverlayPath().to_string().c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200163 }
164
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700165 Result<uint32_t> overlay_crc = GetPackageCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100166 if (!overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100167 return Error("failed to get overlay crc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200168 }
169
Mårten Kongstad0f763112018-11-19 14:14:37 +0100170 if (overlay_crc_ != *overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100171 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
172 *overlay_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200173 }
174
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100175 return Unit{};
Mårten Kongstad02751232018-04-27 13:16:32 +0200176}
177
178std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
179 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
180
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700181 if (!Read8(stream, &idmap_data_header->target_package_id_) ||
182 !Read8(stream, &idmap_data_header->overlay_package_id_) ||
183 !Read32(stream, &idmap_data_header->target_entry_count) ||
184 !Read32(stream, &idmap_data_header->overlay_entry_count) ||
185 !Read32(stream, &idmap_data_header->string_pool_index_offset) ||
186 !Read32(stream, &idmap_data_header->string_pool_len)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200187 return nullptr;
188 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200189
190 return std::move(idmap_data_header);
191}
192
Mårten Kongstad02751232018-04-27 13:16:32 +0200193std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
194 std::unique_ptr<IdmapData> data(new IdmapData());
195 data->header_ = IdmapData::Header::FromBinaryStream(stream);
196 if (!data->header_) {
197 return nullptr;
198 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700199 // Read the mapping of target resource id to overlay resource value.
200 for (size_t i = 0; i < data->header_->GetTargetEntryCount(); i++) {
201 TargetEntry target_entry{};
202 if (!Read32(stream, &target_entry.target_id) || !Read8(stream, &target_entry.data_type) ||
203 !Read32(stream, &target_entry.data_value)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200204 return nullptr;
205 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700206 data->target_entries_.emplace_back(target_entry);
Mårten Kongstad02751232018-04-27 13:16:32 +0200207 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700208
209 // Read the mapping of overlay resource id to target resource id.
210 for (size_t i = 0; i < data->header_->GetOverlayEntryCount(); i++) {
211 OverlayEntry overlay_entry{};
212 if (!Read32(stream, &overlay_entry.overlay_id) || !Read32(stream, &overlay_entry.target_id)) {
213 return nullptr;
214 }
215 data->overlay_entries_.emplace_back(overlay_entry);
216 }
217
218 // Read raw string pool bytes.
219 if (!ReadBuffer(stream, &data->string_pool_, data->header_->string_pool_len)) {
220 return nullptr;
221 }
222
Mårten Kongstad02751232018-04-27 13:16:32 +0200223 return std::move(data);
224}
225
226std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
227 const std::string& absolute_apk_path) {
228 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
229 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
230 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
231 replace(copy.begin(), copy.end(), '/', '@');
232 return absolute_dir + "/" + copy + "@idmap";
233}
234
Mårten Kongstadce424902019-03-01 08:35:37 +0100235Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100236 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200237 std::unique_ptr<Idmap> idmap(new Idmap());
238
239 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
240 if (!idmap->header_) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100241 return Error("failed to parse idmap header");
Mårten Kongstad02751232018-04-27 13:16:32 +0200242 }
243
244 // idmap version 0x01 does not specify the number of data blocks that follow
245 // the idmap header; assume exactly one data block
246 for (int i = 0; i < 1; i++) {
247 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
248 if (!data) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100249 return Error("failed to parse data block %d", i);
Mårten Kongstad02751232018-04-27 13:16:32 +0200250 }
251 idmap->data_.push_back(std::move(data));
252 }
253
Mårten Kongstadce424902019-03-01 08:35:37 +0100254 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200255}
256
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700257Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
258 const ResourceMapping& resource_mapping) {
259 if (resource_mapping.GetTargetToOverlayMap().empty()) {
260 return Error("no resources were overlaid");
261 }
262
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700263 std::unique_ptr<IdmapData> data(new IdmapData());
264 for (const auto& mappings : resource_mapping.GetTargetToOverlayMap()) {
265 data->target_entries_.emplace_back(IdmapData::TargetEntry{
266 mappings.first, mappings.second.data_type, mappings.second.data_value});
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800267 }
268
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700269 for (const auto& mappings : resource_mapping.GetOverlayToTargetMap()) {
270 data->overlay_entries_.emplace_back(IdmapData::OverlayEntry{mappings.first, mappings.second});
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700271 }
272
273 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
274 data_header->target_package_id_ = resource_mapping.GetTargetPackageId();
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700275 data_header->overlay_package_id_ = resource_mapping.GetOverlayPackageId();
276 data_header->target_entry_count = static_cast<uint32_t>(data->target_entries_.size());
277 data_header->overlay_entry_count = static_cast<uint32_t>(data->overlay_entries_.size());
278 data_header->string_pool_index_offset = resource_mapping.GetStringPoolOffset();
279
280 const auto string_pool_data = resource_mapping.GetStringPoolData();
281 data_header->string_pool_len = string_pool_data.second;
282 data->string_pool_ = std::unique_ptr<uint8_t[]>(new uint8_t[data_header->string_pool_len]);
283 memcpy(data->string_pool_.get(), string_pool_data.first, data_header->string_pool_len);
284
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700285 data->header_ = std::move(data_header);
286 return {std::move(data)};
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800287}
288
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700289Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& target_apk_assets,
Mårten Kongstadce424902019-03-01 08:35:37 +0100290 const ApkAssets& overlay_apk_assets,
291 const PolicyBitmask& fulfilled_policies,
292 bool enforce_overlayable) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100293 SYSTRACE << "Idmap::FromApkAssets";
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700294 const std::string& target_apk_path = target_apk_assets.GetPath();
295 const std::string& overlay_apk_path = overlay_apk_assets.GetPath();
Mårten Kongstad02751232018-04-27 13:16:32 +0200296
297 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
298 if (!target_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100299 return Error("failed to open target as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200300 }
301
302 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
303 if (!overlay_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100304 return Error("failed to open overlay as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200305 }
306
307 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
308 header->magic_ = kIdmapMagic;
309 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100310
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700311 Result<uint32_t> crc = GetPackageCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100312 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100313 return Error(crc.GetError(), "failed to get zip CRC for target");
Mårten Kongstad02751232018-04-27 13:16:32 +0200314 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100315 header->target_crc_ = *crc;
316
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700317 crc = GetPackageCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100318 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100319 return Error(crc.GetError(), "failed to get zip CRC for overlay");
Mårten Kongstad02751232018-04-27 13:16:32 +0200320 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100321 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200322
323 if (target_apk_path.size() > sizeof(header->target_path_)) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100324 return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
325 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200326 }
327 memset(header->target_path_, 0, sizeof(header->target_path_));
328 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
329
330 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700331 return Error("overlay apk path \"%s\" longer than maximum size %zu", overlay_apk_path.c_str(),
Mårten Kongstadce424902019-03-01 08:35:37 +0100332 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200333 }
334 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
335 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
336
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700337 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
338 if (!overlay_info) {
339 return overlay_info.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200340 }
341
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200342 LogInfo log_info;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700343 auto resource_mapping =
344 ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *overlay_info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200345 fulfilled_policies, enforce_overlayable, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700346 if (!resource_mapping) {
347 return resource_mapping.GetError();
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800348 }
349
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700350 auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping);
351 if (!idmap_data) {
352 return idmap_data.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200353 }
354
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200355 std::unique_ptr<Idmap> idmap(new Idmap());
356 header->debug_info_ = log_info.GetString();
357 idmap->header_ = std::move(header);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700358 idmap->data_.push_back(std::move(*idmap_data));
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200359
Mårten Kongstadce424902019-03-01 08:35:37 +0100360 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200361}
362
363void IdmapHeader::accept(Visitor* v) const {
364 assert(v != nullptr);
365 v->visit(*this);
366}
367
368void IdmapData::Header::accept(Visitor* v) const {
369 assert(v != nullptr);
370 v->visit(*this);
371}
372
Mårten Kongstad02751232018-04-27 13:16:32 +0200373void IdmapData::accept(Visitor* v) const {
374 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200375 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700376 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200377}
378
379void Idmap::accept(Visitor* v) const {
380 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200381 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700382 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200383 auto end = data_.cend();
384 for (auto iter = data_.cbegin(); iter != end; ++iter) {
385 (*iter)->accept(v);
386 }
387}
388
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100389} // namespace android::idmap2