1 /*
2 * Copyright (C) 2016 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 "androidfw/ApkAssets.h"
18
19 #include "android-base/errors.h"
20 #include "android-base/logging.h"
21 #include "android-base/utf8.h"
22
23 namespace android {
24
25 using base::SystemErrorCodeToString;
26 using base::unique_fd;
27
28 constexpr const char* kResourcesArsc = "resources.arsc";
29
ApkAssets(PrivateConstructorUtil,std::unique_ptr<Asset> resources_asset,std::unique_ptr<LoadedArsc> loaded_arsc,std::unique_ptr<AssetsProvider> assets,package_property_t property_flags,std::unique_ptr<Asset> idmap_asset,std::unique_ptr<LoadedIdmap> loaded_idmap)30 ApkAssets::ApkAssets(PrivateConstructorUtil, std::unique_ptr<Asset> resources_asset,
31 std::unique_ptr<LoadedArsc> loaded_arsc,
32 std::unique_ptr<AssetsProvider> assets, package_property_t property_flags,
33 std::unique_ptr<Asset> idmap_asset, std::unique_ptr<LoadedIdmap> loaded_idmap)
34 : resources_asset_(std::move(resources_asset)),
35 loaded_arsc_(std::move(loaded_arsc)),
36 assets_provider_(std::move(assets)),
37 property_flags_(property_flags),
38 idmap_asset_(std::move(idmap_asset)),
39 loaded_idmap_(std::move(loaded_idmap)) {
40 }
41
Load(const std::string & path,package_property_t flags)42 ApkAssetsPtr ApkAssets::Load(const std::string& path, package_property_t flags) {
43 return Load(ZipAssetsProvider::Create(path, flags), flags);
44 }
45
LoadFromFd(base::unique_fd fd,const std::string & debug_name,package_property_t flags,off64_t offset,off64_t len)46 ApkAssetsPtr ApkAssets::LoadFromFd(base::unique_fd fd, const std::string& debug_name,
47 package_property_t flags, off64_t offset, off64_t len) {
48 return Load(ZipAssetsProvider::Create(std::move(fd), debug_name, offset, len), flags);
49 }
50
Load(std::unique_ptr<AssetsProvider> assets,package_property_t flags)51 ApkAssetsPtr ApkAssets::Load(std::unique_ptr<AssetsProvider> assets, package_property_t flags) {
52 return LoadImpl(std::move(assets), flags, nullptr /* idmap_asset */, nullptr /* loaded_idmap */);
53 }
54
LoadTable(std::unique_ptr<Asset> resources_asset,std::unique_ptr<AssetsProvider> assets,package_property_t flags)55 ApkAssetsPtr ApkAssets::LoadTable(std::unique_ptr<Asset> resources_asset,
56 std::unique_ptr<AssetsProvider> assets,
57 package_property_t flags) {
58 if (resources_asset == nullptr) {
59 return {};
60 }
61 return LoadImpl(std::move(resources_asset), std::move(assets), flags, nullptr /* idmap_asset */,
62 nullptr /* loaded_idmap */);
63 }
64
LoadOverlay(const std::string & idmap_path,package_property_t flags)65 ApkAssetsPtr ApkAssets::LoadOverlay(const std::string& idmap_path, package_property_t flags) {
66 CHECK((flags & PROPERTY_LOADER) == 0U) << "Cannot load RROs through loaders";
67 auto idmap_asset = AssetsProvider::CreateAssetFromFile(idmap_path);
68 if (idmap_asset == nullptr) {
69 LOG(ERROR) << "failed to read IDMAP " << idmap_path;
70 return {};
71 }
72
73 StringPiece idmap_data(reinterpret_cast<const char*>(idmap_asset->getBuffer(true /* aligned */)),
74 static_cast<size_t>(idmap_asset->getLength()));
75 auto loaded_idmap = LoadedIdmap::Load(idmap_path, idmap_data);
76 if (loaded_idmap == nullptr) {
77 LOG(ERROR) << "failed to load IDMAP " << idmap_path;
78 return {};
79 }
80
81 std::string overlay_path(loaded_idmap->OverlayApkPath());
82 auto fd = unique_fd(base::utf8::open(overlay_path.c_str(), O_RDONLY | O_CLOEXEC));
83 std::unique_ptr<AssetsProvider> overlay_assets;
84 if (IsFabricatedOverlayName(overlay_path) && IsFabricatedOverlay(fd)) {
85 // Fabricated overlays do not contain resource definitions. All of the overlay resource values
86 // are defined inline in the idmap.
87 overlay_assets = EmptyAssetsProvider::Create(std::move(overlay_path));
88 } else {
89 // The overlay should be an APK.
90 overlay_assets = ZipAssetsProvider::Create(std::move(overlay_path), flags, std::move(fd));
91 }
92 if (overlay_assets == nullptr) {
93 return {};
94 }
95
96 return LoadImpl(std::move(overlay_assets), flags | PROPERTY_OVERLAY, std::move(idmap_asset),
97 std::move(loaded_idmap));
98 }
99
LoadImpl(std::unique_ptr<AssetsProvider> assets,package_property_t property_flags,std::unique_ptr<Asset> idmap_asset,std::unique_ptr<LoadedIdmap> loaded_idmap)100 ApkAssetsPtr ApkAssets::LoadImpl(std::unique_ptr<AssetsProvider> assets,
101 package_property_t property_flags,
102 std::unique_ptr<Asset> idmap_asset,
103 std::unique_ptr<LoadedIdmap> loaded_idmap) {
104 if (assets == nullptr) {
105 return {};
106 }
107
108 // Open the resource table via mmap unless it is compressed. This logic is taken care of by Open.
109 bool resources_asset_exists = false;
110 auto resources_asset = assets->Open(kResourcesArsc, Asset::AccessMode::ACCESS_BUFFER,
111 &resources_asset_exists);
112 if (resources_asset == nullptr && resources_asset_exists) {
113 LOG(ERROR) << "Failed to open '" << kResourcesArsc << "' in APK '" << assets->GetDebugName()
114 << "'.";
115 return {};
116 }
117
118 return LoadImpl(std::move(resources_asset), std::move(assets), property_flags,
119 std::move(idmap_asset), std::move(loaded_idmap));
120 }
121
LoadImpl(std::unique_ptr<Asset> resources_asset,std::unique_ptr<AssetsProvider> assets,package_property_t property_flags,std::unique_ptr<Asset> idmap_asset,std::unique_ptr<LoadedIdmap> loaded_idmap)122 ApkAssetsPtr ApkAssets::LoadImpl(std::unique_ptr<Asset> resources_asset,
123 std::unique_ptr<AssetsProvider> assets,
124 package_property_t property_flags,
125 std::unique_ptr<Asset> idmap_asset,
126 std::unique_ptr<LoadedIdmap> loaded_idmap) {
127 if (assets == nullptr ) {
128 return {};
129 }
130
131 std::unique_ptr<LoadedArsc> loaded_arsc;
132 if (resources_asset != nullptr) {
133 const auto data = resources_asset->getIncFsBuffer(true /* aligned */);
134 const size_t length = resources_asset->getLength();
135 if (!data || length == 0) {
136 LOG(ERROR) << "Failed to read resources table in APK '" << assets->GetDebugName() << "'.";
137 return {};
138 }
139 loaded_arsc = LoadedArsc::Load(data, length, loaded_idmap.get(), property_flags);
140 } else if (loaded_idmap != nullptr && IsFabricatedOverlay(loaded_idmap->OverlayApkPath())) {
141 loaded_arsc = LoadedArsc::Load(loaded_idmap.get());
142 } else {
143 loaded_arsc = LoadedArsc::CreateEmpty();
144 }
145
146 if (loaded_arsc == nullptr) {
147 LOG(ERROR) << "Failed to load resources table in APK '" << assets->GetDebugName() << "'.";
148 return {};
149 }
150
151 return ApkAssetsPtr::make(PrivateConstructorUtil{}, std::move(resources_asset),
152 std::move(loaded_arsc), std::move(assets), property_flags,
153 std::move(idmap_asset), std::move(loaded_idmap));
154 }
155
GetPath() const156 std::optional<std::string_view> ApkAssets::GetPath() const {
157 return assets_provider_->GetPath();
158 }
159
GetDebugName() const160 const std::string& ApkAssets::GetDebugName() const {
161 return assets_provider_->GetDebugName();
162 }
163
IsUpToDate() const164 bool ApkAssets::IsUpToDate() const {
165 // Loaders are invalidated by the app, not the system, so assume they are up to date.
166 return IsLoader() || ((!loaded_idmap_ || loaded_idmap_->IsUpToDate())
167 && assets_provider_->IsUpToDate());
168 }
169
170 } // namespace android
171