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