1 /*
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
17 #include "idmap2/Idmap.h"
18
19 #include <algorithm>
20 #include <iostream>
21 #include <iterator>
22 #include <limits>
23 #include <memory>
24 #include <string>
25 #include <utility>
26
27 #include "android-base/macros.h"
28 #include "androidfw/AssetManager2.h"
29 #include "idmap2/ResourceMapping.h"
30 #include "idmap2/ResourceUtils.h"
31 #include "idmap2/Result.h"
32 #include "idmap2/SysTrace.h"
33
34 namespace android::idmap2 {
35
36 namespace {
37
Read8(std::istream & stream,uint8_t * out)38 bool WARN_UNUSED Read8(std::istream& stream, uint8_t* out) {
39 uint8_t value;
40 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint8_t))) {
41 *out = value;
42 return true;
43 }
44 return false;
45 }
46
Read16(std::istream & stream,uint16_t * out)47 bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) {
48 uint16_t value;
49 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) {
50 *out = dtohs(value);
51 return true;
52 }
53 return false;
54 }
55
Read32(std::istream & stream,uint32_t * out)56 bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
57 uint32_t value;
58 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
59 *out = dtohl(value);
60 return true;
61 }
62 return false;
63 }
64
ReadString(std::istream & stream,std::string * out)65 bool WARN_UNUSED ReadString(std::istream& stream, std::string* out) {
66 uint32_t size;
67 if (!Read32(stream, &size)) {
68 return false;
69 }
70 if (size == 0) {
71 *out = "";
72 return true;
73 }
74 std::string buf(size, '\0');
75 if (!stream.read(buf.data(), size)) {
76 return false;
77 }
78 uint32_t padding_size = CalculatePadding(size);
79 std::string padding(padding_size, '\0');
80 if (!stream.read(padding.data(), padding_size)) {
81 return false;
82 }
83 *out = buf;
84 return true;
85 }
86
87 } // namespace
88
FromBinaryStream(std::istream & stream)89 std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
90 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
91 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_)) {
92 return nullptr;
93 }
94
95 if (idmap_header->magic_ != kIdmapMagic || idmap_header->version_ != kIdmapCurrentVersion) {
96 // Do not continue parsing if the file is not a current version idmap.
97 return nullptr;
98 }
99
100 uint32_t enforce_overlayable;
101 if (!Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
102 !Read32(stream, &idmap_header->fulfilled_policies_) ||
103 !Read32(stream, &enforce_overlayable) || !ReadString(stream, &idmap_header->target_path_) ||
104 !ReadString(stream, &idmap_header->overlay_path_) ||
105 !ReadString(stream, &idmap_header->overlay_name_) ||
106 !ReadString(stream, &idmap_header->debug_info_)) {
107 return nullptr;
108 }
109
110 idmap_header->enforce_overlayable_ = enforce_overlayable != 0U;
111 return std::move(idmap_header);
112 }
113
IsUpToDate(const TargetResourceContainer & target,const OverlayResourceContainer & overlay,const std::string & overlay_name,PolicyBitmask fulfilled_policies,bool enforce_overlayable) const114 Result<Unit> IdmapHeader::IsUpToDate(const TargetResourceContainer& target,
115 const OverlayResourceContainer& overlay,
116 const std::string& overlay_name,
117 PolicyBitmask fulfilled_policies,
118 bool enforce_overlayable) const {
119 const Result<uint32_t> target_crc = target.GetCrc();
120 if (!target_crc) {
121 return Error("failed to get target crc");
122 }
123
124 const Result<uint32_t> overlay_crc = overlay.GetCrc();
125 if (!overlay_crc) {
126 return Error("failed to get overlay crc");
127 }
128
129 return IsUpToDate(target.GetPath(), overlay.GetPath(), overlay_name, *target_crc, *overlay_crc,
130 fulfilled_policies, enforce_overlayable);
131 }
132
IsUpToDate(const std::string & target_path,const std::string & overlay_path,const std::string & overlay_name,uint32_t target_crc,uint32_t overlay_crc,PolicyBitmask fulfilled_policies,bool enforce_overlayable) const133 Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
134 const std::string& overlay_path,
135 const std::string& overlay_name, uint32_t target_crc,
136 uint32_t overlay_crc, PolicyBitmask fulfilled_policies,
137 bool enforce_overlayable) const {
138 if (magic_ != kIdmapMagic) {
139 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
140 }
141
142 if (version_ != kIdmapCurrentVersion) {
143 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
144 }
145
146 if (target_crc_ != target_crc) {
147 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
148 target_crc);
149 }
150
151 if (overlay_crc_ != overlay_crc) {
152 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
153 overlay_crc);
154 }
155
156 if (fulfilled_policies_ != fulfilled_policies) {
157 return Error("bad fulfilled policies: idmap version 0x%08x, file system version 0x%08x",
158 fulfilled_policies, fulfilled_policies_);
159 }
160
161 if (enforce_overlayable != enforce_overlayable_) {
162 return Error("bad enforce overlayable: idmap version %s, file system version %s",
163 enforce_overlayable ? "true" : "false", enforce_overlayable_ ? "true" : "false");
164 }
165
166 if (target_path != target_path_) {
167 return Error("bad target path: idmap version %s, file system version %s", target_path.c_str(),
168 target_path_.c_str());
169 }
170
171 if (overlay_path != overlay_path_) {
172 return Error("bad overlay path: idmap version %s, file system version %s", overlay_path.c_str(),
173 overlay_path_.c_str());
174 }
175
176 if (overlay_name != overlay_name_) {
177 return Error("bad overlay name: idmap version %s, file system version %s", overlay_name.c_str(),
178 overlay_name_.c_str());
179 }
180
181 return Unit{};
182 }
183
FromBinaryStream(std::istream & stream)184 std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
185 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
186 if (!Read32(stream, &idmap_data_header->target_entry_count) ||
187 !Read32(stream, &idmap_data_header->target_entry_inline_count) ||
188 !Read32(stream, &idmap_data_header->overlay_entry_count) ||
189 !Read32(stream, &idmap_data_header->string_pool_index_offset)) {
190 return nullptr;
191 }
192
193 return std::move(idmap_data_header);
194 }
195
FromBinaryStream(std::istream & stream)196 std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
197 std::unique_ptr<IdmapData> data(new IdmapData());
198 data->header_ = IdmapData::Header::FromBinaryStream(stream);
199 if (!data->header_) {
200 return nullptr;
201 }
202
203 // Read the mapping of target resource id to overlay resource value.
204 for (size_t i = 0; i < data->header_->GetTargetEntryCount(); i++) {
205 TargetEntry target_entry{};
206 if (!Read32(stream, &target_entry.target_id) || !Read32(stream, &target_entry.overlay_id)) {
207 return nullptr;
208 }
209 data->target_entries_.push_back(target_entry);
210 }
211
212 // Read the mapping of target resource id to inline overlay values.
213 uint8_t unused1;
214 uint16_t unused2;
215 for (size_t i = 0; i < data->header_->GetTargetInlineEntryCount(); i++) {
216 TargetInlineEntry target_entry{};
217 if (!Read32(stream, &target_entry.target_id) || !Read16(stream, &unused2) ||
218 !Read8(stream, &unused1) || !Read8(stream, &target_entry.value.data_type) ||
219 !Read32(stream, &target_entry.value.data_value)) {
220 return nullptr;
221 }
222 data->target_inline_entries_.push_back(target_entry);
223 }
224
225 // Read the mapping of overlay resource id to target resource id.
226 for (size_t i = 0; i < data->header_->GetOverlayEntryCount(); i++) {
227 OverlayEntry overlay_entry{};
228 if (!Read32(stream, &overlay_entry.overlay_id) || !Read32(stream, &overlay_entry.target_id)) {
229 return nullptr;
230 }
231 data->overlay_entries_.emplace_back(overlay_entry);
232 }
233
234 // Read raw string pool bytes.
235 if (!ReadString(stream, &data->string_pool_data_)) {
236 return nullptr;
237 }
238 return std::move(data);
239 }
240
CanonicalIdmapPathFor(const std::string & absolute_dir,const std::string & absolute_apk_path)241 std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
242 const std::string& absolute_apk_path) {
243 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
244 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
245 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
246 replace(copy.begin(), copy.end(), '/', '@');
247 return absolute_dir + "/" + copy + "@idmap";
248 }
249
FromBinaryStream(std::istream & stream)250 Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
251 SYSTRACE << "Idmap::FromBinaryStream";
252 std::unique_ptr<Idmap> idmap(new Idmap());
253
254 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
255 if (!idmap->header_) {
256 return Error("failed to parse idmap header");
257 }
258
259 // idmap version 0x01 does not specify the number of data blocks that follow
260 // the idmap header; assume exactly one data block
261 for (int i = 0; i < 1; i++) {
262 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
263 if (!data) {
264 return Error("failed to parse data block %d", i);
265 }
266 idmap->data_.push_back(std::move(data));
267 }
268
269 return {std::move(idmap)};
270 }
271
FromResourceMapping(const ResourceMapping & resource_mapping)272 Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
273 const ResourceMapping& resource_mapping) {
274 if (resource_mapping.GetTargetToOverlayMap().empty()) {
275 return Error("no resources were overlaid");
276 }
277
278 std::unique_ptr<IdmapData> data(new IdmapData());
279 data->string_pool_data_ = resource_mapping.GetStringPoolData().to_string();
280 for (const auto& mapping : resource_mapping.GetTargetToOverlayMap()) {
281 if (auto overlay_resource = std::get_if<ResourceId>(&mapping.second)) {
282 data->target_entries_.push_back({mapping.first, *overlay_resource});
283 } else {
284 data->target_inline_entries_.push_back(
285 {mapping.first, std::get<TargetValue>(mapping.second)});
286 }
287 }
288
289 for (const auto& mapping : resource_mapping.GetOverlayToTargetMap()) {
290 data->overlay_entries_.emplace_back(IdmapData::OverlayEntry{mapping.first, mapping.second});
291 }
292
293 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
294 data_header->target_entry_count = static_cast<uint32_t>(data->target_entries_.size());
295 data_header->target_entry_inline_count =
296 static_cast<uint32_t>(data->target_inline_entries_.size());
297 data_header->overlay_entry_count = static_cast<uint32_t>(data->overlay_entries_.size());
298 data_header->string_pool_index_offset = resource_mapping.GetStringPoolOffset();
299 data->header_ = std::move(data_header);
300 return {std::move(data)};
301 }
302
FromContainers(const TargetResourceContainer & target,const OverlayResourceContainer & overlay,const std::string & overlay_name,const PolicyBitmask & fulfilled_policies,bool enforce_overlayable)303 Result<std::unique_ptr<const Idmap>> Idmap::FromContainers(const TargetResourceContainer& target,
304 const OverlayResourceContainer& overlay,
305 const std::string& overlay_name,
306 const PolicyBitmask& fulfilled_policies,
307 bool enforce_overlayable) {
308 SYSTRACE << "Idmap::FromApkAssets";
309 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
310 header->magic_ = kIdmapMagic;
311 header->version_ = kIdmapCurrentVersion;
312
313 const auto target_crc = target.GetCrc();
314 if (!target_crc) {
315 return Error(target_crc.GetError(), "failed to get zip CRC for '%s'", target.GetPath().data());
316 }
317 header->target_crc_ = *target_crc;
318
319 const auto overlay_crc = overlay.GetCrc();
320 if (!overlay_crc) {
321 return Error(overlay_crc.GetError(), "failed to get zip CRC for '%s'",
322 overlay.GetPath().data());
323 }
324 header->overlay_crc_ = *overlay_crc;
325
326 header->fulfilled_policies_ = fulfilled_policies;
327 header->enforce_overlayable_ = enforce_overlayable;
328 header->target_path_ = target.GetPath();
329 header->overlay_path_ = overlay.GetPath();
330 header->overlay_name_ = overlay_name;
331
332 auto info = overlay.FindOverlayInfo(overlay_name);
333 if (!info) {
334 return Error(info.GetError(), "failed to get overlay info for '%s'", overlay.GetPath().data());
335 }
336
337 LogInfo log_info;
338 auto resource_mapping = ResourceMapping::FromContainers(
339 target, overlay, *info, fulfilled_policies, enforce_overlayable, log_info);
340 if (!resource_mapping) {
341 return Error(resource_mapping.GetError(), "failed to generate resource map for '%s'",
342 overlay.GetPath().data());
343 }
344
345 auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping);
346 if (!idmap_data) {
347 return idmap_data.GetError();
348 }
349
350 std::unique_ptr<Idmap> idmap(new Idmap());
351 header->debug_info_ = log_info.GetString();
352 idmap->header_ = std::move(header);
353 idmap->data_.push_back(std::move(*idmap_data));
354
355 return {std::move(idmap)};
356 }
357
accept(Visitor * v) const358 void IdmapHeader::accept(Visitor* v) const {
359 assert(v != nullptr);
360 v->visit(*this);
361 }
362
accept(Visitor * v) const363 void IdmapData::Header::accept(Visitor* v) const {
364 assert(v != nullptr);
365 v->visit(*this);
366 }
367
accept(Visitor * v) const368 void IdmapData::accept(Visitor* v) const {
369 assert(v != nullptr);
370 header_->accept(v);
371 v->visit(*this);
372 }
373
accept(Visitor * v) const374 void Idmap::accept(Visitor* v) const {
375 assert(v != nullptr);
376 header_->accept(v);
377 v->visit(*this);
378 auto end = data_.cend();
379 for (auto iter = data_.cbegin(); iter != end; ++iter) {
380 (*iter)->accept(v);
381 }
382 }
383
384 } // namespace android::idmap2
385