blob: f82c8f1af71394033ec98a826df2ab4bd0e51509 [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"
Winson62ac8b52019-12-04 08:36:48 -080027#include "androidfw/ResourceTypes.h"
28#include "idmap2/PolicyUtils.h"
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070029#include "idmap2/ResourceUtils.h"
30
31using android::base::StringPrintf;
Winson62ac8b52019-12-04 08:36:48 -080032using android::idmap2::utils::BitmaskToPolicies;
Ryan Mitchell5035d662020-01-22 13:19:41 -080033using android::idmap2::utils::IsReference;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070034using android::idmap2::utils::ResToTypeEntryName;
Winson62ac8b52019-12-04 08:36:48 -080035using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
36using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070037
38namespace android::idmap2 {
39
40namespace {
41
Ryan Mitchellee4a5642019-10-16 08:32:55 -070042#define REWRITE_PACKAGE(resid, package_id) \
43 (((resid)&0x00ffffffU) | (((uint32_t)(package_id)) << 24U))
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070044#define EXTRACT_PACKAGE(resid) ((0xff000000 & (resid)) >> 24)
45
46std::string ConcatPolicies(const std::vector<std::string>& policies) {
47 std::string message;
48 for (const std::string& policy : policies) {
49 if (!message.empty()) {
50 message.append("|");
51 }
52 message.append(policy);
53 }
54
55 return message;
56}
57
58Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
59 const OverlayManifestInfo& overlay_info,
60 const PolicyBitmask& fulfilled_policies,
61 const ResourceId& target_resource) {
62 static constexpr const PolicyBitmask sDefaultPolicies =
Winson62ac8b52019-12-04 08:36:48 -080063 PolicyFlags::ODM_PARTITION | PolicyFlags::OEM_PARTITION | PolicyFlags::SYSTEM_PARTITION |
64 PolicyFlags::VENDOR_PARTITION | PolicyFlags::PRODUCT_PARTITION | PolicyFlags::SIGNATURE;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070065
66 // If the resource does not have an overlayable definition, allow the resource to be overlaid if
67 // the overlay is preinstalled or signed with the same signature as the target.
68 if (!target_package.DefinesOverlayable()) {
69 return (sDefaultPolicies & fulfilled_policies) != 0
70 ? Result<Unit>({})
71 : Error(
72 "overlay must be preinstalled or signed with the same signature as the "
73 "target");
74 }
75
76 const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(target_resource);
77 if (overlayable_info == nullptr) {
78 // Do not allow non-overlayable resources to be overlaid.
79 return Error("target resource has no overlayable declaration");
80 }
81
82 if (overlay_info.target_name != overlayable_info->name) {
83 // If the overlay supplies a target overlayable name, the resource must belong to the
84 // overlayable defined with the specified name to be overlaid.
85 return Error(R"(<overlay> android:targetName "%s" does not match overlayable name "%s")",
86 overlay_info.target_name.c_str(), overlayable_info->name.c_str());
87 }
88
89 // Enforce policy restrictions if the resource is declared as overlayable.
90 if ((overlayable_info->policy_flags & fulfilled_policies) == 0) {
91 return Error(R"(overlay with policies "%s" does not fulfill any overlayable policies "%s")",
92 ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(),
93 ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str());
94 }
95
96 return Result<Unit>({});
97}
98
99// TODO(martenkongstad): scan for package name instead of assuming package at index 0
100//
101// idmap version 0x01 naively assumes that the package to use is always the first ResTable_package
102// in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so
103// this assumption tends to work out. That said, the correct thing to do is to scan
104// resources.arsc for a package with a given name as read from the package manifest instead of
105// relying on a hard-coded index. This however requires storing the package name in the idmap
106// header, which in turn requires incrementing the idmap version. Because the initial version of
107// idmap2 is compatible with idmap, this will have to wait for now.
108const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) {
109 const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc.GetPackages();
110 if (packages.empty()) {
111 return nullptr;
112 }
113 int id = packages[0]->GetPackageId();
114 return loaded_arsc.GetPackageById(id);
115}
116
117Result<std::unique_ptr<Asset>> OpenNonAssetFromResource(const ResourceId& resource_id,
118 const AssetManager2& asset_manager) {
119 Res_value value{};
120 ResTable_config selected_config{};
121 uint32_t flags;
122 auto cookie =
123 asset_manager.GetResource(resource_id, /* may_be_bag */ false,
124 /* density_override */ 0U, &value, &selected_config, &flags);
125 if (cookie == kInvalidCookie) {
126 return Error("failed to find resource for id 0x%08x", resource_id);
127 }
128
129 if (value.dataType != Res_value::TYPE_STRING) {
130 return Error("resource for is 0x%08x is not a file", resource_id);
131 }
132
133 auto string_pool = asset_manager.GetStringPoolForCookie(cookie);
134 size_t len;
135 auto file_path16 = string_pool->stringAt(value.data, &len);
136 if (file_path16 == nullptr) {
137 return Error("failed to find string for index %d", value.data);
138 }
139
140 // Load the overlay resource mappings from the file specified using android:resourcesMap.
141 auto file_path = String8(String16(file_path16));
142 auto asset = asset_manager.OpenNonAsset(file_path.c_str(), Asset::AccessMode::ACCESS_BUFFER);
143 if (asset == nullptr) {
144 return Error("file \"%s\" not found", file_path.c_str());
145 }
146
147 return asset;
148}
149
150} // namespace
151
152Result<ResourceMapping> ResourceMapping::CreateResourceMapping(const AssetManager2* target_am,
153 const LoadedPackage* target_package,
154 const LoadedPackage* overlay_package,
155 size_t string_pool_offset,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200156 const XmlParser& overlay_parser,
157 LogInfo& log_info) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700158 ResourceMapping resource_mapping;
159 auto root_it = overlay_parser.tree_iterator();
160 if (root_it->event() != XmlParser::Event::START_TAG || root_it->name() != "overlay") {
161 return Error("root element is not <overlay> tag");
162 }
163
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700164 const uint8_t target_package_id = target_package->GetPackageId();
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700165 const uint8_t overlay_package_id = overlay_package->GetPackageId();
166 auto overlay_it_end = root_it.end();
167 for (auto overlay_it = root_it.begin(); overlay_it != overlay_it_end; ++overlay_it) {
168 if (overlay_it->event() == XmlParser::Event::BAD_DOCUMENT) {
169 return Error("failed to parse overlay xml document");
170 }
171
172 if (overlay_it->event() != XmlParser::Event::START_TAG) {
173 continue;
174 }
175
176 if (overlay_it->name() != "item") {
177 return Error("unexpected tag <%s> in <overlay>", overlay_it->name().c_str());
178 }
179
180 Result<std::string> target_resource = overlay_it->GetAttributeStringValue("target");
181 if (!target_resource) {
182 return Error(R"(<item> tag missing expected attribute "target")");
183 }
184
185 Result<android::Res_value> overlay_resource = overlay_it->GetAttributeValue("value");
186 if (!overlay_resource) {
187 return Error(R"(<item> tag missing expected attribute "value")");
188 }
189
190 ResourceId target_id =
191 target_am->GetResourceId(*target_resource, "", target_package->GetPackageName());
192 if (target_id == 0U) {
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200193 log_info.Warning(LogMessage() << "failed to find resource \"" << *target_resource
194 << "\" in target resources");
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700195 continue;
196 }
197
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700198 // Retrieve the compile-time resource id of the target resource.
199 target_id = REWRITE_PACKAGE(target_id, target_package_id);
200
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700201 if (overlay_resource->dataType == Res_value::TYPE_STRING) {
202 overlay_resource->data += string_pool_offset;
203 }
204
205 // Only rewrite resources defined within the overlay package to their corresponding target
206 // resource ids at runtime.
207 bool rewrite_overlay_reference =
Ryan Mitchell5035d662020-01-22 13:19:41 -0800208 IsReference(overlay_resource->dataType)
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700209 ? overlay_package_id == EXTRACT_PACKAGE(overlay_resource->data)
210 : false;
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200211
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700212 if (rewrite_overlay_reference) {
213 overlay_resource->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
214 }
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700215
216 resource_mapping.AddMapping(target_id, overlay_resource->dataType, overlay_resource->data,
217 rewrite_overlay_reference);
218 }
219
220 return resource_mapping;
221}
222
223Result<ResourceMapping> ResourceMapping::CreateResourceMappingLegacy(
224 const AssetManager2* target_am, const AssetManager2* overlay_am,
225 const LoadedPackage* target_package, const LoadedPackage* overlay_package) {
226 ResourceMapping resource_mapping;
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700227 const uint8_t target_package_id = target_package->GetPackageId();
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700228 const auto end = overlay_package->end();
229 for (auto iter = overlay_package->begin(); iter != end; ++iter) {
230 const ResourceId overlay_resid = *iter;
231 Result<std::string> name = utils::ResToTypeEntryName(*overlay_am, overlay_resid);
232 if (!name) {
233 continue;
234 }
235
236 // Find the resource with the same type and entry name within the target package.
237 const std::string full_name =
238 base::StringPrintf("%s:%s", target_package->GetPackageName().c_str(), name->c_str());
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700239 ResourceId target_resource = target_am->GetResourceId(full_name);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700240 if (target_resource == 0U) {
241 continue;
242 }
243
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700244 // Retrieve the compile-time resource id of the target resource.
245 target_resource = REWRITE_PACKAGE(target_resource, target_package_id);
246
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700247 resource_mapping.AddMapping(target_resource, Res_value::TYPE_REFERENCE, overlay_resid,
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700248 /* rewrite_overlay_reference */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700249 }
250
251 return resource_mapping;
252}
253
254void ResourceMapping::FilterOverlayableResources(const AssetManager2* target_am,
255 const LoadedPackage* target_package,
256 const LoadedPackage* overlay_package,
257 const OverlayManifestInfo& overlay_info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200258 const PolicyBitmask& fulfilled_policies,
259 LogInfo& log_info) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700260 std::set<ResourceId> remove_ids;
261 for (const auto& target_map : target_map_) {
262 const ResourceId target_resid = target_map.first;
263 Result<Unit> success =
264 CheckOverlayable(*target_package, overlay_info, fulfilled_policies, target_resid);
265 if (success) {
266 continue;
267 }
268
269 // Attempting to overlay a resource that is not allowed to be overlaid is treated as a
270 // warning.
271 Result<std::string> name = utils::ResToTypeEntryName(*target_am, target_resid);
272 if (!name) {
273 name = StringPrintf("0x%08x", target_resid);
274 }
275
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200276 log_info.Warning(LogMessage() << "overlay \"" << overlay_package->GetPackageName()
277 << "\" is not allowed to overlay resource \"" << *name
278 << "\" in target: " << success.GetErrorMessage());
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700279
280 remove_ids.insert(target_resid);
281 }
282
283 for (const ResourceId target_resid : remove_ids) {
284 RemoveMapping(target_resid);
285 }
286}
287
288Result<ResourceMapping> ResourceMapping::FromApkAssets(const ApkAssets& target_apk_assets,
289 const ApkAssets& overlay_apk_assets,
290 const OverlayManifestInfo& overlay_info,
291 const PolicyBitmask& fulfilled_policies,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200292 bool enforce_overlayable,
293 LogInfo& log_info) {
294 if (enforce_overlayable) {
295 log_info.Info(LogMessage() << "fulfilled_policies="
296 << ConcatPolicies(BitmaskToPolicies(fulfilled_policies))
297 << " enforce_overlayable="
298 << (enforce_overlayable ? "true" : "false"));
299 }
300
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700301 AssetManager2 target_asset_manager;
302 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true /* invalidate_caches */,
303 false /* filter_incompatible_configs*/)) {
304 return Error("failed to create target asset manager");
305 }
306
307 AssetManager2 overlay_asset_manager;
308 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true /* invalidate_caches */,
309 false /* filter_incompatible_configs */)) {
310 return Error("failed to create overlay asset manager");
311 }
312
313 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
314 if (target_arsc == nullptr) {
315 return Error("failed to load target resources.arsc");
316 }
317
318 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
319 if (overlay_arsc == nullptr) {
320 return Error("failed to load overlay resources.arsc");
321 }
322
323 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
324 if (target_pkg == nullptr) {
325 return Error("failed to load target package from resources.arsc");
326 }
327
328 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
329 if (overlay_pkg == nullptr) {
330 return Error("failed to load overlay package from resources.arsc");
331 }
332
333 size_t string_pool_data_length = 0U;
334 size_t string_pool_offset = 0U;
335 std::unique_ptr<uint8_t[]> string_pool_data;
336 Result<ResourceMapping> resource_mapping = {{}};
337 if (overlay_info.resource_mapping != 0U) {
Ryan Mitchell5035d662020-01-22 13:19:41 -0800338 // Use the dynamic reference table to find the assigned resource id of the map xml.
339 const auto& ref_table = overlay_asset_manager.GetDynamicRefTableForCookie(0);
340 uint32_t resource_mapping_id = overlay_info.resource_mapping;
341 ref_table->lookupResourceId(&resource_mapping_id);
342
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700343 // Load the overlay resource mappings from the file specified using android:resourcesMap.
Ryan Mitchell5035d662020-01-22 13:19:41 -0800344 auto asset = OpenNonAssetFromResource(resource_mapping_id, overlay_asset_manager);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700345 if (!asset) {
346 return Error("failed opening xml for android:resourcesMap: %s",
347 asset.GetErrorMessage().c_str());
348 }
349
350 auto parser =
351 XmlParser::Create((*asset)->getBuffer(true /* wordAligned*/), (*asset)->getLength());
352 if (!parser) {
353 return Error("failed opening ResXMLTree");
354 }
355
356 // Copy the xml string pool data before the parse goes out of scope.
357 auto& string_pool = (*parser)->get_strings();
358 string_pool_data_length = string_pool.bytes();
359 string_pool_data.reset(new uint8_t[string_pool_data_length]);
360 memcpy(string_pool_data.get(), string_pool.data(), string_pool_data_length);
361
362 // Offset string indices by the size of the overlay resource table string pool.
363 string_pool_offset = overlay_arsc->GetStringPool()->size();
364
365 resource_mapping = CreateResourceMapping(&target_asset_manager, target_pkg, overlay_pkg,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200366 string_pool_offset, *(*parser), log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700367 } else {
368 // If no file is specified using android:resourcesMap, it is assumed that the overlay only
369 // defines resources intended to override target resources of the same type and name.
370 resource_mapping = CreateResourceMappingLegacy(&target_asset_manager, &overlay_asset_manager,
371 target_pkg, overlay_pkg);
372 }
373
374 if (!resource_mapping) {
375 return resource_mapping.GetError();
376 }
377
378 if (enforce_overlayable) {
379 // Filter out resources the overlay is not allowed to override.
380 (*resource_mapping)
381 .FilterOverlayableResources(&target_asset_manager, target_pkg, overlay_pkg, overlay_info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200382 fulfilled_policies, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700383 }
384
385 resource_mapping->target_package_id_ = target_pkg->GetPackageId();
386 resource_mapping->overlay_package_id_ = overlay_pkg->GetPackageId();
387 resource_mapping->string_pool_offset_ = string_pool_offset;
388 resource_mapping->string_pool_data_ = std::move(string_pool_data);
389 resource_mapping->string_pool_data_length_ = string_pool_data_length;
390 return std::move(*resource_mapping);
391}
392
393OverlayResourceMap ResourceMapping::GetOverlayToTargetMap() const {
394 // An overlay resource can override multiple target resources at once. Rewrite the overlay
395 // resource as the first target resource it overrides.
396 OverlayResourceMap map;
397 for (const auto& mappings : overlay_map_) {
398 map.insert(std::make_pair(mappings.first, mappings.second));
399 }
400 return map;
401}
402
403Result<Unit> ResourceMapping::AddMapping(ResourceId target_resource,
404 TargetValue::DataType data_type,
405 TargetValue::DataValue data_value,
406 bool rewrite_overlay_reference) {
407 if (target_map_.find(target_resource) != target_map_.end()) {
408 return Error(R"(target resource id "0x%08x" mapped to multiple values)", target_resource);
409 }
410
411 // TODO(141485591): Ensure that the overlay type is compatible with the target type. If the
412 // runtime types are not compatible, it could cause runtime crashes when the resource is resolved.
413
414 target_map_.insert(std::make_pair(target_resource, TargetValue{data_type, data_value}));
415
Ryan Mitchell5035d662020-01-22 13:19:41 -0800416 if (rewrite_overlay_reference && IsReference(data_type)) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700417 overlay_map_.insert(std::make_pair(data_value, target_resource));
418 }
419
420 return Result<Unit>({});
421}
422
423void ResourceMapping::RemoveMapping(ResourceId target_resource) {
424 auto target_iter = target_map_.find(target_resource);
425 if (target_iter == target_map_.end()) {
426 return;
427 }
428
429 const TargetValue value = target_iter->second;
430 target_map_.erase(target_iter);
431
Ryan Mitchell5035d662020-01-22 13:19:41 -0800432 if (!IsReference(value.data_type)) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700433 return;
434 }
435
436 auto overlay_iter = overlay_map_.equal_range(value.data_value);
437 for (auto i = overlay_iter.first; i != overlay_iter.second; ++i) {
438 if (i->second == target_resource) {
439 overlay_map_.erase(i);
440 return;
441 }
442 }
443}
444
445} // namespace android::idmap2