blob: 43cfec3f9cf92b8a69d6b84e1dd973b52c5d26ed [file] [log] [blame]
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -07001/*
2 * Copyright (C) 2019 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/ResourceMapping.h"
18
19#include <map>
20#include <memory>
21#include <set>
22#include <string>
23#include <utility>
24#include <vector>
25
26#include "android-base/stringprintf.h"
27#include "idmap2/ResourceUtils.h"
28
29using android::base::StringPrintf;
Ryan Mitchell5035d662020-01-22 13:19:41 -080030using android::idmap2::utils::IsReference;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070031using android::idmap2::utils::ResToTypeEntryName;
32
33namespace android::idmap2 {
34
35namespace {
36
Ryan Mitchellee4a5642019-10-16 08:32:55 -070037#define REWRITE_PACKAGE(resid, package_id) \
38 (((resid)&0x00ffffffU) | (((uint32_t)(package_id)) << 24U))
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070039#define EXTRACT_PACKAGE(resid) ((0xff000000 & (resid)) >> 24)
40
41std::string ConcatPolicies(const std::vector<std::string>& policies) {
42 std::string message;
43 for (const std::string& policy : policies) {
44 if (!message.empty()) {
45 message.append("|");
46 }
47 message.append(policy);
48 }
49
50 return message;
51}
52
53Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
54 const OverlayManifestInfo& overlay_info,
55 const PolicyBitmask& fulfilled_policies,
56 const ResourceId& target_resource) {
57 static constexpr const PolicyBitmask sDefaultPolicies =
58 PolicyFlags::POLICY_ODM_PARTITION | PolicyFlags::POLICY_OEM_PARTITION |
59 PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION |
60 PolicyFlags::POLICY_PRODUCT_PARTITION | PolicyFlags::POLICY_SIGNATURE;
61
62 // If the resource does not have an overlayable definition, allow the resource to be overlaid if
63 // the overlay is preinstalled or signed with the same signature as the target.
64 if (!target_package.DefinesOverlayable()) {
65 return (sDefaultPolicies & fulfilled_policies) != 0
66 ? Result<Unit>({})
67 : Error(
68 "overlay must be preinstalled or signed with the same signature as the "
69 "target");
70 }
71
72 const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(target_resource);
73 if (overlayable_info == nullptr) {
74 // Do not allow non-overlayable resources to be overlaid.
75 return Error("target resource has no overlayable declaration");
76 }
77
78 if (overlay_info.target_name != overlayable_info->name) {
79 // If the overlay supplies a target overlayable name, the resource must belong to the
80 // overlayable defined with the specified name to be overlaid.
81 return Error(R"(<overlay> android:targetName "%s" does not match overlayable name "%s")",
82 overlay_info.target_name.c_str(), overlayable_info->name.c_str());
83 }
84
85 // Enforce policy restrictions if the resource is declared as overlayable.
86 if ((overlayable_info->policy_flags & fulfilled_policies) == 0) {
87 return Error(R"(overlay with policies "%s" does not fulfill any overlayable policies "%s")",
88 ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(),
89 ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str());
90 }
91
92 return Result<Unit>({});
93}
94
95// TODO(martenkongstad): scan for package name instead of assuming package at index 0
96//
97// idmap version 0x01 naively assumes that the package to use is always the first ResTable_package
98// in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so
99// this assumption tends to work out. That said, the correct thing to do is to scan
100// resources.arsc for a package with a given name as read from the package manifest instead of
101// relying on a hard-coded index. This however requires storing the package name in the idmap
102// header, which in turn requires incrementing the idmap version. Because the initial version of
103// idmap2 is compatible with idmap, this will have to wait for now.
104const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) {
105 const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc.GetPackages();
106 if (packages.empty()) {
107 return nullptr;
108 }
109 int id = packages[0]->GetPackageId();
110 return loaded_arsc.GetPackageById(id);
111}
112
113Result<std::unique_ptr<Asset>> OpenNonAssetFromResource(const ResourceId& resource_id,
114 const AssetManager2& asset_manager) {
115 Res_value value{};
116 ResTable_config selected_config{};
117 uint32_t flags;
118 auto cookie =
119 asset_manager.GetResource(resource_id, /* may_be_bag */ false,
120 /* density_override */ 0U, &value, &selected_config, &flags);
121 if (cookie == kInvalidCookie) {
122 return Error("failed to find resource for id 0x%08x", resource_id);
123 }
124
125 if (value.dataType != Res_value::TYPE_STRING) {
126 return Error("resource for is 0x%08x is not a file", resource_id);
127 }
128
129 auto string_pool = asset_manager.GetStringPoolForCookie(cookie);
130 size_t len;
131 auto file_path16 = string_pool->stringAt(value.data, &len);
132 if (file_path16 == nullptr) {
133 return Error("failed to find string for index %d", value.data);
134 }
135
136 // Load the overlay resource mappings from the file specified using android:resourcesMap.
137 auto file_path = String8(String16(file_path16));
138 auto asset = asset_manager.OpenNonAsset(file_path.c_str(), Asset::AccessMode::ACCESS_BUFFER);
139 if (asset == nullptr) {
140 return Error("file \"%s\" not found", file_path.c_str());
141 }
142
143 return asset;
144}
145
146} // namespace
147
148Result<ResourceMapping> ResourceMapping::CreateResourceMapping(const AssetManager2* target_am,
149 const LoadedPackage* target_package,
150 const LoadedPackage* overlay_package,
151 size_t string_pool_offset,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200152 const XmlParser& overlay_parser,
153 LogInfo& log_info) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700154 ResourceMapping resource_mapping;
155 auto root_it = overlay_parser.tree_iterator();
156 if (root_it->event() != XmlParser::Event::START_TAG || root_it->name() != "overlay") {
157 return Error("root element is not <overlay> tag");
158 }
159
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700160 const uint8_t target_package_id = target_package->GetPackageId();
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700161 const uint8_t overlay_package_id = overlay_package->GetPackageId();
162 auto overlay_it_end = root_it.end();
163 for (auto overlay_it = root_it.begin(); overlay_it != overlay_it_end; ++overlay_it) {
164 if (overlay_it->event() == XmlParser::Event::BAD_DOCUMENT) {
165 return Error("failed to parse overlay xml document");
166 }
167
168 if (overlay_it->event() != XmlParser::Event::START_TAG) {
169 continue;
170 }
171
172 if (overlay_it->name() != "item") {
173 return Error("unexpected tag <%s> in <overlay>", overlay_it->name().c_str());
174 }
175
176 Result<std::string> target_resource = overlay_it->GetAttributeStringValue("target");
177 if (!target_resource) {
178 return Error(R"(<item> tag missing expected attribute "target")");
179 }
180
181 Result<android::Res_value> overlay_resource = overlay_it->GetAttributeValue("value");
182 if (!overlay_resource) {
183 return Error(R"(<item> tag missing expected attribute "value")");
184 }
185
186 ResourceId target_id =
187 target_am->GetResourceId(*target_resource, "", target_package->GetPackageName());
188 if (target_id == 0U) {
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200189 log_info.Warning(LogMessage() << "failed to find resource \"" << *target_resource
190 << "\" in target resources");
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700191 continue;
192 }
193
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700194 // Retrieve the compile-time resource id of the target resource.
195 target_id = REWRITE_PACKAGE(target_id, target_package_id);
196
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700197 if (overlay_resource->dataType == Res_value::TYPE_STRING) {
198 overlay_resource->data += string_pool_offset;
199 }
200
201 // Only rewrite resources defined within the overlay package to their corresponding target
202 // resource ids at runtime.
203 bool rewrite_overlay_reference =
Ryan Mitchell5035d662020-01-22 13:19:41 -0800204 IsReference(overlay_resource->dataType)
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700205 ? overlay_package_id == EXTRACT_PACKAGE(overlay_resource->data)
206 : false;
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200207
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700208 if (rewrite_overlay_reference) {
209 overlay_resource->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
210 }
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700211
212 resource_mapping.AddMapping(target_id, overlay_resource->dataType, overlay_resource->data,
213 rewrite_overlay_reference);
214 }
215
216 return resource_mapping;
217}
218
219Result<ResourceMapping> ResourceMapping::CreateResourceMappingLegacy(
220 const AssetManager2* target_am, const AssetManager2* overlay_am,
221 const LoadedPackage* target_package, const LoadedPackage* overlay_package) {
222 ResourceMapping resource_mapping;
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700223 const uint8_t target_package_id = target_package->GetPackageId();
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700224 const auto end = overlay_package->end();
225 for (auto iter = overlay_package->begin(); iter != end; ++iter) {
226 const ResourceId overlay_resid = *iter;
227 Result<std::string> name = utils::ResToTypeEntryName(*overlay_am, overlay_resid);
228 if (!name) {
229 continue;
230 }
231
232 // Find the resource with the same type and entry name within the target package.
233 const std::string full_name =
234 base::StringPrintf("%s:%s", target_package->GetPackageName().c_str(), name->c_str());
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700235 ResourceId target_resource = target_am->GetResourceId(full_name);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700236 if (target_resource == 0U) {
237 continue;
238 }
239
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700240 // Retrieve the compile-time resource id of the target resource.
241 target_resource = REWRITE_PACKAGE(target_resource, target_package_id);
242
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700243 resource_mapping.AddMapping(target_resource, Res_value::TYPE_REFERENCE, overlay_resid,
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700244 /* rewrite_overlay_reference */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700245 }
246
247 return resource_mapping;
248}
249
250void ResourceMapping::FilterOverlayableResources(const AssetManager2* target_am,
251 const LoadedPackage* target_package,
252 const LoadedPackage* overlay_package,
253 const OverlayManifestInfo& overlay_info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200254 const PolicyBitmask& fulfilled_policies,
255 LogInfo& log_info) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700256 std::set<ResourceId> remove_ids;
257 for (const auto& target_map : target_map_) {
258 const ResourceId target_resid = target_map.first;
259 Result<Unit> success =
260 CheckOverlayable(*target_package, overlay_info, fulfilled_policies, target_resid);
261 if (success) {
262 continue;
263 }
264
265 // Attempting to overlay a resource that is not allowed to be overlaid is treated as a
266 // warning.
267 Result<std::string> name = utils::ResToTypeEntryName(*target_am, target_resid);
268 if (!name) {
269 name = StringPrintf("0x%08x", target_resid);
270 }
271
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200272 log_info.Warning(LogMessage() << "overlay \"" << overlay_package->GetPackageName()
273 << "\" is not allowed to overlay resource \"" << *name
274 << "\" in target: " << success.GetErrorMessage());
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700275
276 remove_ids.insert(target_resid);
277 }
278
279 for (const ResourceId target_resid : remove_ids) {
280 RemoveMapping(target_resid);
281 }
282}
283
284Result<ResourceMapping> ResourceMapping::FromApkAssets(const ApkAssets& target_apk_assets,
285 const ApkAssets& overlay_apk_assets,
286 const OverlayManifestInfo& overlay_info,
287 const PolicyBitmask& fulfilled_policies,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200288 bool enforce_overlayable,
289 LogInfo& log_info) {
290 if (enforce_overlayable) {
291 log_info.Info(LogMessage() << "fulfilled_policies="
292 << ConcatPolicies(BitmaskToPolicies(fulfilled_policies))
293 << " enforce_overlayable="
294 << (enforce_overlayable ? "true" : "false"));
295 }
296
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700297 AssetManager2 target_asset_manager;
298 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true /* invalidate_caches */,
299 false /* filter_incompatible_configs*/)) {
300 return Error("failed to create target asset manager");
301 }
302
303 AssetManager2 overlay_asset_manager;
304 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true /* invalidate_caches */,
305 false /* filter_incompatible_configs */)) {
306 return Error("failed to create overlay asset manager");
307 }
308
309 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
310 if (target_arsc == nullptr) {
311 return Error("failed to load target resources.arsc");
312 }
313
314 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
315 if (overlay_arsc == nullptr) {
316 return Error("failed to load overlay resources.arsc");
317 }
318
319 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
320 if (target_pkg == nullptr) {
321 return Error("failed to load target package from resources.arsc");
322 }
323
324 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
325 if (overlay_pkg == nullptr) {
326 return Error("failed to load overlay package from resources.arsc");
327 }
328
329 size_t string_pool_data_length = 0U;
330 size_t string_pool_offset = 0U;
331 std::unique_ptr<uint8_t[]> string_pool_data;
332 Result<ResourceMapping> resource_mapping = {{}};
333 if (overlay_info.resource_mapping != 0U) {
Ryan Mitchell5035d662020-01-22 13:19:41 -0800334 // Use the dynamic reference table to find the assigned resource id of the map xml.
335 const auto& ref_table = overlay_asset_manager.GetDynamicRefTableForCookie(0);
336 uint32_t resource_mapping_id = overlay_info.resource_mapping;
337 ref_table->lookupResourceId(&resource_mapping_id);
338
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700339 // Load the overlay resource mappings from the file specified using android:resourcesMap.
Ryan Mitchell5035d662020-01-22 13:19:41 -0800340 auto asset = OpenNonAssetFromResource(resource_mapping_id, overlay_asset_manager);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700341 if (!asset) {
342 return Error("failed opening xml for android:resourcesMap: %s",
343 asset.GetErrorMessage().c_str());
344 }
345
346 auto parser =
347 XmlParser::Create((*asset)->getBuffer(true /* wordAligned*/), (*asset)->getLength());
348 if (!parser) {
349 return Error("failed opening ResXMLTree");
350 }
351
352 // Copy the xml string pool data before the parse goes out of scope.
353 auto& string_pool = (*parser)->get_strings();
354 string_pool_data_length = string_pool.bytes();
355 string_pool_data.reset(new uint8_t[string_pool_data_length]);
356 memcpy(string_pool_data.get(), string_pool.data(), string_pool_data_length);
357
358 // Offset string indices by the size of the overlay resource table string pool.
359 string_pool_offset = overlay_arsc->GetStringPool()->size();
360
361 resource_mapping = CreateResourceMapping(&target_asset_manager, target_pkg, overlay_pkg,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200362 string_pool_offset, *(*parser), log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700363 } else {
364 // If no file is specified using android:resourcesMap, it is assumed that the overlay only
365 // defines resources intended to override target resources of the same type and name.
366 resource_mapping = CreateResourceMappingLegacy(&target_asset_manager, &overlay_asset_manager,
367 target_pkg, overlay_pkg);
368 }
369
370 if (!resource_mapping) {
371 return resource_mapping.GetError();
372 }
373
374 if (enforce_overlayable) {
375 // Filter out resources the overlay is not allowed to override.
376 (*resource_mapping)
377 .FilterOverlayableResources(&target_asset_manager, target_pkg, overlay_pkg, overlay_info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200378 fulfilled_policies, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700379 }
380
381 resource_mapping->target_package_id_ = target_pkg->GetPackageId();
382 resource_mapping->overlay_package_id_ = overlay_pkg->GetPackageId();
383 resource_mapping->string_pool_offset_ = string_pool_offset;
384 resource_mapping->string_pool_data_ = std::move(string_pool_data);
385 resource_mapping->string_pool_data_length_ = string_pool_data_length;
386 return std::move(*resource_mapping);
387}
388
389OverlayResourceMap ResourceMapping::GetOverlayToTargetMap() const {
390 // An overlay resource can override multiple target resources at once. Rewrite the overlay
391 // resource as the first target resource it overrides.
392 OverlayResourceMap map;
393 for (const auto& mappings : overlay_map_) {
394 map.insert(std::make_pair(mappings.first, mappings.second));
395 }
396 return map;
397}
398
399Result<Unit> ResourceMapping::AddMapping(ResourceId target_resource,
400 TargetValue::DataType data_type,
401 TargetValue::DataValue data_value,
402 bool rewrite_overlay_reference) {
403 if (target_map_.find(target_resource) != target_map_.end()) {
404 return Error(R"(target resource id "0x%08x" mapped to multiple values)", target_resource);
405 }
406
407 // TODO(141485591): Ensure that the overlay type is compatible with the target type. If the
408 // runtime types are not compatible, it could cause runtime crashes when the resource is resolved.
409
410 target_map_.insert(std::make_pair(target_resource, TargetValue{data_type, data_value}));
411
Ryan Mitchell5035d662020-01-22 13:19:41 -0800412 if (rewrite_overlay_reference && IsReference(data_type)) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700413 overlay_map_.insert(std::make_pair(data_value, target_resource));
414 }
415
416 return Result<Unit>({});
417}
418
419void ResourceMapping::RemoveMapping(ResourceId target_resource) {
420 auto target_iter = target_map_.find(target_resource);
421 if (target_iter == target_map_.end()) {
422 return;
423 }
424
425 const TargetValue value = target_iter->second;
426 target_map_.erase(target_iter);
427
Ryan Mitchell5035d662020-01-22 13:19:41 -0800428 if (!IsReference(value.data_type)) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700429 return;
430 }
431
432 auto overlay_iter = overlay_map_.equal_range(value.data_value);
433 for (auto i = overlay_iter.first; i != overlay_iter.second; ++i) {
434 if (i->second == target_resource) {
435 overlay_map_.erase(i);
436 return;
437 }
438 }
439}
440
441} // namespace android::idmap2