1 /*
2 * Copyright (C) 2021 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/ResourceContainer.h"
18
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "android-base/scopeguard.h"
26 #include "androidfw/ApkAssets.h"
27 #include "androidfw/AssetManager.h"
28 #include "androidfw/Util.h"
29 #include "idmap2/FabricatedOverlay.h"
30 #include "idmap2/XmlParser.h"
31
32 namespace android::idmap2 {
33 namespace {
34 #define REWRITE_PACKAGE(resid, package_id) \
35 (((resid)&0x00ffffffU) | (((uint32_t)(package_id)) << 24U))
36
37 #define EXTRACT_PACKAGE(resid) ((0xff000000 & (resid)) >> 24)
38
39 constexpr ResourceId kAttrName = 0x01010003;
40 constexpr ResourceId kAttrResourcesMap = 0x01010609;
41 constexpr ResourceId kAttrTargetName = 0x0101044d;
42 constexpr ResourceId kAttrTargetPackage = 0x01010021;
43
44 // idmap version 0x01 naively assumes that the package to use is always the first ResTable_package
45 // in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so
46 // this assumption tends to work out. That said, the correct thing to do is to scan
47 // resources.arsc for a package with a given name as read from the package manifest instead of
48 // relying on a hard-coded index. This however requires storing the package name in the idmap
49 // header, which in turn requires incrementing the idmap version. Because the initial version of
50 // idmap2 is compatible with idmap, this will have to wait for now.
GetPackageAtIndex0(const LoadedArsc * loaded_arsc)51 const LoadedPackage* GetPackageAtIndex0(const LoadedArsc* loaded_arsc) {
52 const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc->GetPackages();
53 if (packages.empty()) {
54 return nullptr;
55 }
56 return loaded_arsc->GetPackageById(packages[0]->GetPackageId());
57 }
58
CalculateCrc(const ZipAssetsProvider * zip_assets)59 Result<uint32_t> CalculateCrc(const ZipAssetsProvider* zip_assets) {
60 constexpr const char* kResourcesArsc = "resources.arsc";
61 std::optional<uint32_t> res_crc = zip_assets->GetCrc(kResourcesArsc);
62 if (!res_crc) {
63 return Error("failed to get CRC for '%s'", kResourcesArsc);
64 }
65
66 constexpr const char* kManifest = "AndroidManifest.xml";
67 std::optional<uint32_t> man_crc = zip_assets->GetCrc(kManifest);
68 if (!man_crc) {
69 return Error("failed to get CRC for '%s'", kManifest);
70 }
71
72 return *res_crc ^ *man_crc;
73 }
74
OpenXmlParser(const std::string & entry_path,const ZipAssetsProvider * zip)75 Result<XmlParser> OpenXmlParser(const std::string& entry_path, const ZipAssetsProvider* zip) {
76 auto manifest = zip->Open(entry_path);
77 if (manifest == nullptr) {
78 return Error("failed to find %s ", entry_path.c_str());
79 }
80
81 auto size = manifest->getLength();
82 auto buffer = manifest->getIncFsBuffer(true /* aligned */).convert<uint8_t>();
83 if (!buffer.verify(size)) {
84 return Error("failed to read entire %s", entry_path.c_str());
85 }
86
87 return XmlParser::Create(buffer.unsafe_ptr(), size, true /* copyData */);
88 }
89
OpenXmlParser(ResourceId id,const ZipAssetsProvider * zip,const AssetManager2 * am)90 Result<XmlParser> OpenXmlParser(ResourceId id, const ZipAssetsProvider* zip,
91 const AssetManager2* am) {
92 const auto ref_table = am->GetDynamicRefTableForCookie(0);
93 if (ref_table == nullptr) {
94 return Error("failed to find dynamic ref table for cookie 0");
95 }
96
97 ref_table->lookupResourceId(&id);
98 auto value = am->GetResource(id);
99 if (!value.has_value()) {
100 return Error("failed to find resource for id 0x%08x", id);
101 }
102
103 if (value->type != Res_value::TYPE_STRING) {
104 return Error("resource for is 0x%08x is not a file", id);
105 }
106
107 auto string_pool = am->GetStringPoolForCookie(value->cookie);
108 auto file = string_pool->string8ObjectAt(value->data);
109 if (!file.has_value()) {
110 return Error("failed to find string for index %d", value->data);
111 }
112
113 return OpenXmlParser(file->c_str(), zip);
114 }
115
ExtractOverlayManifestInfo(const ZipAssetsProvider * zip,const std::string & name)116 Result<OverlayManifestInfo> ExtractOverlayManifestInfo(const ZipAssetsProvider* zip,
117 const std::string& name) {
118 Result<XmlParser> xml = OpenXmlParser("AndroidManifest.xml", zip);
119 if (!xml) {
120 return xml.GetError();
121 }
122
123 auto manifest_it = xml->tree_iterator();
124 if (manifest_it->event() != XmlParser::Event::START_TAG || manifest_it->name() != "manifest") {
125 return Error("root element tag is not <manifest> in AndroidManifest.xml");
126 }
127
128 std::string package_name;
129 if (auto result_str = manifest_it->GetAttributeStringValue("package")) {
130 package_name = *result_str;
131 } else {
132 return result_str.GetError();
133 }
134
135 for (auto&& it : manifest_it) {
136 if (it.event() != XmlParser::Event::START_TAG || it.name() != "overlay") {
137 continue;
138 }
139
140 OverlayManifestInfo info{};
141 info.package_name = package_name;
142 if (auto result_str = it.GetAttributeStringValue(kAttrName, "android:name")) {
143 if (*result_str != name) {
144 // A value for android:name was found, but either a the name does not match the requested
145 // name, or an <overlay> tag with no name was requested.
146 continue;
147 }
148 info.name = *result_str;
149 } else if (!name.empty()) {
150 // This tag does not have a value for android:name, but an <overlay> tag with a specific name
151 // has been requested.
152 continue;
153 }
154
155 if (auto result_str = it.GetAttributeStringValue(kAttrTargetPackage, "android:targetPackage")) {
156 info.target_package = *result_str;
157 } else {
158 return Error("android:targetPackage missing from <overlay> in AndroidManifest.xml");
159 }
160
161 if (auto result_str = it.GetAttributeStringValue(kAttrTargetName, "android:targetName")) {
162 info.target_name = *result_str;
163 }
164
165 if (auto result_value = it.GetAttributeValue(kAttrResourcesMap, "android:resourcesMap")) {
166 if (utils::IsReference((*result_value).dataType)) {
167 info.resource_mapping = (*result_value).data;
168 } else {
169 return Error("android:resourcesMap is not a reference in AndroidManifest.xml");
170 }
171 }
172 return info;
173 }
174
175 return Error("<overlay> with android:name \"%s\" missing from AndroidManifest.xml", name.c_str());
176 }
177
CreateResourceMapping(ResourceId id,const ZipAssetsProvider * zip,const AssetManager2 * overlay_am,const LoadedArsc * overlay_arsc,const LoadedPackage * overlay_package)178 Result<OverlayData> CreateResourceMapping(ResourceId id, const ZipAssetsProvider* zip,
179 const AssetManager2* overlay_am,
180 const LoadedArsc* overlay_arsc,
181 const LoadedPackage* overlay_package) {
182 auto parser = OpenXmlParser(id, zip, overlay_am);
183 if (!parser) {
184 return parser.GetError();
185 }
186
187 OverlayData overlay_data{};
188 const uint32_t string_pool_offset = overlay_arsc->GetStringPool()->size();
189 const uint8_t package_id = overlay_package->GetPackageId();
190 auto root_it = parser->tree_iterator();
191 if (root_it->event() != XmlParser::Event::START_TAG || root_it->name() != "overlay") {
192 return Error("root element is not <overlay> tag");
193 }
194
195 auto overlay_it_end = root_it.end();
196 for (auto overlay_it = root_it.begin(); overlay_it != overlay_it_end; ++overlay_it) {
197 if (overlay_it->event() == XmlParser::Event::BAD_DOCUMENT) {
198 return Error("failed to parse overlay xml document");
199 }
200
201 if (overlay_it->event() != XmlParser::Event::START_TAG) {
202 continue;
203 }
204
205 if (overlay_it->name() != "item") {
206 return Error("unexpected tag <%s> in <overlay>", overlay_it->name().c_str());
207 }
208
209 Result<std::string> target_resource = overlay_it->GetAttributeStringValue("target");
210 if (!target_resource) {
211 return Error(R"(<item> tag missing expected attribute "target")");
212 }
213
214 Result<android::Res_value> overlay_resource = overlay_it->GetAttributeValue("value");
215 if (!overlay_resource) {
216 return Error(R"(<item> tag missing expected attribute "value")");
217 }
218
219 if (overlay_resource->dataType == Res_value::TYPE_STRING) {
220 overlay_resource->data += string_pool_offset;
221 }
222
223 if (utils::IsReference(overlay_resource->dataType)) {
224 // Only rewrite resources defined within the overlay package to their corresponding target
225 // resource ids at runtime.
226 bool rewrite_id = package_id == EXTRACT_PACKAGE(overlay_resource->data);
227 overlay_data.pairs.emplace_back(OverlayData::Value{
228 *target_resource, OverlayData::ResourceIdValue{overlay_resource->data, rewrite_id}});
229 } else {
230 overlay_data.pairs.emplace_back(
231 OverlayData::Value{*target_resource, TargetValueWithConfig{
232 .value = TargetValue{.data_type = overlay_resource->dataType,
233 .data_value = overlay_resource->data},
234 .config = std::string()}});
235 }
236 }
237
238 const auto& string_pool = parser->get_strings();
239 const uint32_t string_pool_data_length = string_pool.bytes();
240 overlay_data.string_pool_data = OverlayData::InlineStringPoolData{
241 .data = std::unique_ptr<uint8_t[]>(new uint8_t[string_pool_data_length]),
242 .data_length = string_pool_data_length,
243 .string_pool_offset = string_pool_offset,
244 };
245
246 // Overlays should not be incrementally installed, so calling unsafe_ptr is fine here.
247 memcpy(overlay_data.string_pool_data->data.get(), string_pool.data().unsafe_ptr(),
248 string_pool_data_length);
249 return overlay_data;
250 }
251
CreateResourceMappingLegacy(const AssetManager2 * overlay_am,const LoadedPackage * overlay_package)252 OverlayData CreateResourceMappingLegacy(const AssetManager2* overlay_am,
253 const LoadedPackage* overlay_package) {
254 OverlayData overlay_data{};
255 for (const ResourceId overlay_resid : *overlay_package) {
256 if (auto name = utils::ResToTypeEntryName(*overlay_am, overlay_resid)) {
257 // Disable rewriting. Overlays did not support internal references before
258 // android:resourcesMap. Do not introduce new behavior.
259 overlay_data.pairs.emplace_back(OverlayData::Value{
260 *name, OverlayData::ResourceIdValue{overlay_resid, false /* rewrite_id */}});
261 }
262 }
263 return overlay_data;
264 }
265
266 struct ResState {
267 AssetManager2::ApkAssetsPtr apk_assets;
268 const LoadedArsc* arsc;
269 const LoadedPackage* package;
270 std::unique_ptr<AssetManager2> am;
271 ZipAssetsProvider* zip_assets;
272
Initializeandroid::idmap2::__anonf19543d00111::ResState273 static Result<ResState> Initialize(std::unique_ptr<ZipAssetsProvider>&& zip,
274 package_property_t flags) {
275 ResState state;
276 state.zip_assets = zip.get();
277 if ((state.apk_assets = ApkAssets::Load(std::move(zip), flags)) == nullptr) {
278 return Error("failed to load apk asset for '%s'",
279 state.zip_assets->GetDebugName().c_str());
280 }
281
282 // Make sure we put ZipAssetsProvider where we took it if initialization fails, so the
283 // original object stays valid for any next call it may get.
284 auto scoped_restore_zip_assets = android::base::ScopeGuard([&zip, &state]() {
285 zip = std::unique_ptr<ZipAssetsProvider>(
286 static_cast<ZipAssetsProvider*>(
287 std::move(const_cast<ApkAssets&>(*state.apk_assets)).TakeAssetsProvider().release()));
288 });
289
290 if ((state.arsc = state.apk_assets->GetLoadedArsc()) == nullptr) {
291 return Error("failed to retrieve loaded arsc for '%s'",
292 state.zip_assets->GetDebugName().c_str());
293 }
294
295 if ((state.package = GetPackageAtIndex0(state.arsc)) == nullptr) {
296 return Error("failed to retrieve loaded package at index 0 for '%s'",
297 state.zip_assets->GetDebugName().c_str());
298 }
299
300 state.am = std::make_unique<AssetManager2>();
301 if (!state.am->SetApkAssets({state.apk_assets}, false)) {
302 return Error("failed to create asset manager for '%s'",
303 state.zip_assets->GetDebugName().c_str());
304 }
305
306 scoped_restore_zip_assets.Disable();
307 return state;
308 }
309 };
310
311 } // namespace
312
313 struct ApkResourceContainer : public TargetResourceContainer, public OverlayResourceContainer {
314 static Result<std::unique_ptr<ApkResourceContainer>> FromPath(std::string path);
315
316 // inherited from TargetResourceContainer
317 Result<bool> DefinesOverlayable() const override;
318 Result<const android::OverlayableInfo*> GetOverlayableInfo(ResourceId id) const override;
319 Result<ResourceId> GetResourceId(const std::string& name) const override;
320
321 // inherited from OverlayResourceContainer
322 Result<OverlayData> GetOverlayData(const OverlayManifestInfo& info) const override;
323 Result<OverlayManifestInfo> FindOverlayInfo(const std::string& name) const override;
324
325 // inherited from ResourceContainer
326 Result<uint32_t> GetCrc() const override;
327 Result<std::string> GetResourceName(ResourceId id) const override;
328 const std::string& GetPath() const override;
329
330 ~ApkResourceContainer() override = default;
331
332 private:
333 ApkResourceContainer(std::unique_ptr<ZipAssetsProvider> zip_assets, std::string path);
334
335 Result<const ResState*> GetState() const;
336 ZipAssetsProvider* GetZipAssets() const;
337
338 mutable std::mutex state_lock_;
339 mutable std::variant<std::unique_ptr<ZipAssetsProvider>, ResState> state_;
340 std::string path_;
341 };
342
ApkResourceContainer(std::unique_ptr<ZipAssetsProvider> zip_assets,std::string path)343 ApkResourceContainer::ApkResourceContainer(std::unique_ptr<ZipAssetsProvider> zip_assets,
344 std::string path)
345 : state_(std::move(zip_assets)), path_(std::move(path)) {
346 }
347
FromPath(std::string path)348 Result<std::unique_ptr<ApkResourceContainer>> ApkResourceContainer::FromPath(
349 std::string path) {
350 auto zip_assets = ZipAssetsProvider::Create(path, 0 /* flags */);
351 if (zip_assets == nullptr) {
352 return Error("failed to load zip assets");
353 }
354 return std::unique_ptr<ApkResourceContainer>(
355 new ApkResourceContainer(std::move(zip_assets), std::move(path)));
356 }
357
GetState() const358 Result<const ResState*> ApkResourceContainer::GetState() const {
359 std::lock_guard lock(state_lock_);
360 if (auto state = std::get_if<ResState>(&state_); state != nullptr) {
361 return state;
362 }
363
364 auto state = ResState::Initialize(std::move(std::get<std::unique_ptr<ZipAssetsProvider>>(state_)),
365 PROPERTY_OPTIMIZE_NAME_LOOKUPS);
366 if (!state) {
367 return state.GetError();
368 }
369
370 state_ = std::move(*state);
371 return &std::get<ResState>(state_);
372 }
373
GetZipAssets() const374 ZipAssetsProvider* ApkResourceContainer::GetZipAssets() const {
375 std::lock_guard lock(state_lock_);
376 if (auto zip = std::get_if<std::unique_ptr<ZipAssetsProvider>>(&state_); zip != nullptr) {
377 return zip->get();
378 }
379 return std::get<ResState>(state_).zip_assets;
380 }
381
DefinesOverlayable() const382 Result<bool> ApkResourceContainer::DefinesOverlayable() const {
383 auto state = GetState();
384 if (!state) {
385 return state.GetError();
386 }
387 return (*state)->package->DefinesOverlayable();
388 }
389
GetOverlayableInfo(ResourceId id) const390 Result<const android::OverlayableInfo*> ApkResourceContainer::GetOverlayableInfo(
391 ResourceId id) const {
392 auto state = GetState();
393 if (!state) {
394 return state.GetError();
395 }
396 return (*state)->package->GetOverlayableInfo(id);
397 }
398
FindOverlayInfo(const std::string & name) const399 Result<OverlayManifestInfo> ApkResourceContainer::FindOverlayInfo(const std::string& name) const {
400 return ExtractOverlayManifestInfo(GetZipAssets(), name);
401 }
402
GetOverlayData(const OverlayManifestInfo & info) const403 Result<OverlayData> ApkResourceContainer::GetOverlayData(const OverlayManifestInfo& info) const {
404 const auto state = GetState();
405 if (!state) {
406 return state.GetError();
407 }
408
409 if (info.resource_mapping != 0) {
410 return CreateResourceMapping(info.resource_mapping, GetZipAssets(), (*state)->am.get(),
411 (*state)->arsc, (*state)->package);
412 }
413 return CreateResourceMappingLegacy((*state)->am.get(), (*state)->package);
414 }
415
GetCrc() const416 Result<uint32_t> ApkResourceContainer::GetCrc() const {
417 return CalculateCrc(GetZipAssets());
418 }
419
GetPath() const420 const std::string& ApkResourceContainer::GetPath() const {
421 return path_;
422 }
423
GetResourceId(const std::string & name) const424 Result<ResourceId> ApkResourceContainer::GetResourceId(const std::string& name) const {
425 auto state = GetState();
426 if (!state) {
427 return state.GetError();
428 }
429 auto id = (*state)->am->GetResourceId(name, "", (*state)->package->GetPackageName());
430 if (!id.has_value()) {
431 return Error("failed to find resource '%s'", name.c_str());
432 }
433
434 // Retrieve the compile-time resource id of the target resource.
435 return REWRITE_PACKAGE(*id, (*state)->package->GetPackageId());
436 }
437
GetResourceName(ResourceId id) const438 Result<std::string> ApkResourceContainer::GetResourceName(ResourceId id) const {
439 auto state = GetState();
440 if (!state) {
441 return state.GetError();
442 }
443 return utils::ResToTypeEntryName(*(*state)->am, id);
444 }
445
FromPath(std::string path)446 Result<std::unique_ptr<TargetResourceContainer>> TargetResourceContainer::FromPath(
447 std::string path) {
448 auto result = ApkResourceContainer::FromPath(std::move(path));
449 if (!result) {
450 return result.GetError();
451 }
452 return std::unique_ptr<TargetResourceContainer>(result->release());
453 }
454
FromPath(std::string path)455 Result<std::unique_ptr<OverlayResourceContainer>> OverlayResourceContainer::FromPath(
456 std::string path) {
457 // Load the path as a fabricated overlay if the file magic indicates this is a fabricated overlay.
458 if (android::IsFabricatedOverlay(path)) {
459 auto result = FabricatedOverlayContainer::FromPath(std::move(path));
460 if (!result) {
461 return result.GetError();
462 }
463 return std::unique_ptr<OverlayResourceContainer>(result->release());
464 }
465
466 // Fallback to loading the container as an APK.
467 auto result = ApkResourceContainer::FromPath(std::move(path));
468 if (!result) {
469 return result.GetError();
470 }
471 return std::unique_ptr<OverlayResourceContainer>(result->release());
472 }
473
474 } // namespace android::idmap2
475