Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/Idmap.h" |
| 18 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <iostream> |
| 21 | #include <iterator> |
| 22 | #include <limits> |
| 23 | #include <map> |
| 24 | #include <memory> |
| 25 | #include <set> |
| 26 | #include <string> |
| 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "android-base/macros.h" |
| 31 | #include "android-base/stringprintf.h" |
| 32 | #include "androidfw/AssetManager2.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 33 | #include "idmap2/ResourceUtils.h" |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 34 | #include "idmap2/Result.h" |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 35 | #include "idmap2/SysTrace.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 36 | #include "idmap2/ZipFile.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 37 | #include "utils/String16.h" |
| 38 | #include "utils/String8.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 39 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 40 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 41 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 42 | namespace { |
| 43 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 44 | #define EXTRACT_TYPE(resid) ((0x00ff0000 & (resid)) >> 16) |
| 45 | |
| 46 | #define EXTRACT_ENTRY(resid) (0x0000ffff & (resid)) |
| 47 | |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 48 | class MatchingResources { |
| 49 | public: |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 50 | void Add(ResourceId target_resid, ResourceId overlay_resid) { |
| 51 | TypeId target_typeid = EXTRACT_TYPE(target_resid); |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 52 | if (map_.find(target_typeid) == map_.end()) { |
| 53 | map_.emplace(target_typeid, std::set<std::pair<ResourceId, ResourceId>>()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 54 | } |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 55 | map_[target_typeid].insert(std::make_pair(target_resid, overlay_resid)); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Yi Kong | 974d516 | 2019-03-06 16:18:11 -0800 | [diff] [blame] | 58 | inline const std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>>& WARN_UNUSED |
Mårten Kongstad | cf62249 | 2019-03-19 22:40:58 +0100 | [diff] [blame] | 59 | Map() const { |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 60 | return map_; |
| 61 | } |
| 62 | |
| 63 | private: |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 64 | // target type id -> set { pair { overlay entry id, overlay entry id } } |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 65 | std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>> map_; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 68 | bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 69 | uint16_t value; |
| 70 | if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) { |
| 71 | *out = dtohl(value); |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 77 | bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 78 | uint32_t value; |
| 79 | if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) { |
| 80 | *out = dtohl(value); |
| 81 | return true; |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // a string is encoded as a kIdmapStringLength char array; the array is always null-terminated |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 87 | bool WARN_UNUSED ReadString(std::istream& stream, char out[kIdmapStringLength]) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 88 | char buf[kIdmapStringLength]; |
| 89 | memset(buf, 0, sizeof(buf)); |
| 90 | if (!stream.read(buf, sizeof(buf))) { |
| 91 | return false; |
| 92 | } |
| 93 | if (buf[sizeof(buf) - 1] != '\0') { |
| 94 | return false; |
| 95 | } |
| 96 | memcpy(out, buf, sizeof(buf)); |
| 97 | return true; |
| 98 | } |
| 99 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 100 | ResourceId NameToResid(const AssetManager2& am, const std::string& name) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 101 | return am.GetResourceId(name); |
| 102 | } |
| 103 | |
| 104 | // TODO(martenkongstad): scan for package name instead of assuming package at index 0 |
| 105 | // |
| 106 | // idmap version 0x01 naively assumes that the package to use is always the first ResTable_package |
| 107 | // in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so |
| 108 | // this assumption tends to work out. That said, the correct thing to do is to scan |
| 109 | // resources.arsc for a package with a given name as read from the package manifest instead of |
| 110 | // relying on a hard-coded index. This however requires storing the package name in the idmap |
| 111 | // header, which in turn requires incrementing the idmap version. Because the initial version of |
| 112 | // idmap2 is compatible with idmap, this will have to wait for now. |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 113 | const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 114 | const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc.GetPackages(); |
| 115 | if (packages.empty()) { |
| 116 | return nullptr; |
| 117 | } |
| 118 | int id = packages[0]->GetPackageId(); |
| 119 | return loaded_arsc.GetPackageById(id); |
| 120 | } |
| 121 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 122 | Result<uint32_t> GetCrc(const ZipFile& zip) { |
| 123 | const Result<uint32_t> a = zip.Crc("resources.arsc"); |
| 124 | const Result<uint32_t> b = zip.Crc("AndroidManifest.xml"); |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 125 | return a && b |
| 126 | ? Result<uint32_t>(*a ^ *b) |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 127 | : Error("failed to get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc"); |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 130 | } // namespace |
| 131 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 132 | std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) { |
| 133 | std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader()); |
| 134 | |
| 135 | if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) || |
| 136 | !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) || |
| 137 | !ReadString(stream, idmap_header->target_path_) || |
| 138 | !ReadString(stream, idmap_header->overlay_path_)) { |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| 142 | return std::move(idmap_header); |
| 143 | } |
| 144 | |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 145 | Result<Unit> IdmapHeader::IsUpToDate() const { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 146 | if (magic_ != kIdmapMagic) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 147 | return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | if (version_ != kIdmapCurrentVersion) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 151 | return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_); |
| 155 | if (!target_zip) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 156 | return Error("failed to open target %s", GetTargetPath().to_string().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 159 | Result<uint32_t> target_crc = GetCrc(*target_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 160 | if (!target_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 161 | return Error("failed to get target crc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 164 | if (target_crc_ != *target_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 165 | return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_, |
| 166 | *target_crc); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_); |
| 170 | if (!overlay_zip) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 171 | return Error("failed to open overlay %s", GetOverlayPath().to_string().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 174 | Result<uint32_t> overlay_crc = GetCrc(*overlay_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 175 | if (!overlay_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 176 | return Error("failed to get overlay crc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 179 | if (overlay_crc_ != *overlay_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 180 | return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_, |
| 181 | *overlay_crc); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 184 | return Unit{}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) { |
| 188 | std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header()); |
| 189 | |
| 190 | uint16_t target_package_id16; |
| 191 | if (!Read16(stream, &target_package_id16) || !Read16(stream, &idmap_data_header->type_count_)) { |
| 192 | return nullptr; |
| 193 | } |
| 194 | idmap_data_header->target_package_id_ = target_package_id16; |
| 195 | |
| 196 | return std::move(idmap_data_header); |
| 197 | } |
| 198 | |
| 199 | std::unique_ptr<const IdmapData::TypeEntry> IdmapData::TypeEntry::FromBinaryStream( |
| 200 | std::istream& stream) { |
| 201 | std::unique_ptr<IdmapData::TypeEntry> data(new IdmapData::TypeEntry()); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 202 | uint16_t target_type16; |
| 203 | uint16_t overlay_type16; |
| 204 | uint16_t entry_count; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 205 | if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) || |
| 206 | !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) { |
| 207 | return nullptr; |
| 208 | } |
| 209 | data->target_type_id_ = target_type16; |
| 210 | data->overlay_type_id_ = overlay_type16; |
| 211 | for (uint16_t i = 0; i < entry_count; i++) { |
| 212 | ResourceId resid; |
| 213 | if (!Read32(stream, &resid)) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | data->entries_.push_back(resid); |
| 217 | } |
| 218 | |
| 219 | return std::move(data); |
| 220 | } |
| 221 | |
| 222 | std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) { |
| 223 | std::unique_ptr<IdmapData> data(new IdmapData()); |
| 224 | data->header_ = IdmapData::Header::FromBinaryStream(stream); |
| 225 | if (!data->header_) { |
| 226 | return nullptr; |
| 227 | } |
| 228 | for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) { |
| 229 | std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream); |
| 230 | if (!type) { |
| 231 | return nullptr; |
| 232 | } |
| 233 | data->type_entries_.push_back(std::move(type)); |
| 234 | } |
| 235 | return std::move(data); |
| 236 | } |
| 237 | |
| 238 | std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir, |
| 239 | const std::string& absolute_apk_path) { |
| 240 | assert(absolute_dir.size() > 0 && absolute_dir[0] == "/"); |
| 241 | assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/"); |
| 242 | std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend()); |
| 243 | replace(copy.begin(), copy.end(), '/', '@'); |
| 244 | return absolute_dir + "/" + copy + "@idmap"; |
| 245 | } |
| 246 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 247 | Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) { |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 248 | SYSTRACE << "Idmap::FromBinaryStream"; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 249 | std::unique_ptr<Idmap> idmap(new Idmap()); |
| 250 | |
| 251 | idmap->header_ = IdmapHeader::FromBinaryStream(stream); |
| 252 | if (!idmap->header_) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 253 | return Error("failed to parse idmap header"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // idmap version 0x01 does not specify the number of data blocks that follow |
| 257 | // the idmap header; assume exactly one data block |
| 258 | for (int i = 0; i < 1; i++) { |
| 259 | std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream); |
| 260 | if (!data) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 261 | return Error("failed to parse data block %d", i); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 262 | } |
| 263 | idmap->data_.push_back(std::move(data)); |
| 264 | } |
| 265 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 266 | return {std::move(idmap)}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 269 | std::string ConcatPolicies(const std::vector<std::string>& policies) { |
| 270 | std::string message; |
| 271 | for (const std::string& policy : policies) { |
Ryan Mitchell | ac791e6 | 2019-04-03 11:02:39 -0700 | [diff] [blame] | 272 | if (!message.empty()) { |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 273 | message.append("|"); |
| 274 | } |
Ryan Mitchell | ac791e6 | 2019-04-03 11:02:39 -0700 | [diff] [blame] | 275 | message.append(policy); |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return message; |
| 279 | } |
| 280 | |
| 281 | Result<Unit> CheckOverlayable(const LoadedPackage& target_package, |
| 282 | const utils::OverlayManifestInfo& overlay_info, |
| 283 | const PolicyBitmask& fulfilled_policies, const ResourceId& resid) { |
Ryan Mitchell | b863ca3 | 2019-03-07 14:31:54 -0800 | [diff] [blame] | 284 | static constexpr const PolicyBitmask sDefaultPolicies = |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 285 | PolicyFlags::POLICY_ODM_PARTITION | PolicyFlags::POLICY_OEM_PARTITION | |
Ryan Mitchell | b863ca3 | 2019-03-07 14:31:54 -0800 | [diff] [blame] | 286 | PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION | |
| 287 | PolicyFlags::POLICY_PRODUCT_PARTITION | PolicyFlags::POLICY_SIGNATURE; |
| 288 | |
| 289 | // If the resource does not have an overlayable definition, allow the resource to be overlaid if |
| 290 | // the overlay is preinstalled or signed with the same signature as the target. |
| 291 | if (!target_package.DefinesOverlayable()) { |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 292 | return (sDefaultPolicies & fulfilled_policies) != 0 |
| 293 | ? Result<Unit>({}) |
| 294 | : Error( |
| 295 | "overlay must be preinstalled or signed with the same signature as the " |
| 296 | "target"); |
Ryan Mitchell | b863ca3 | 2019-03-07 14:31:54 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 299 | const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(resid); |
| 300 | if (overlayable_info == nullptr) { |
Ryan Mitchell | b863ca3 | 2019-03-07 14:31:54 -0800 | [diff] [blame] | 301 | // Do not allow non-overlayable resources to be overlaid. |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 302 | return Error("resource has no overlayable declaration"); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Ryan Mitchell | 1982345 | 2019-01-29 12:01:24 -0800 | [diff] [blame] | 305 | if (overlay_info.target_name != overlayable_info->name) { |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 306 | // If the overlay supplies a target overlayable name, the resource must belong to the |
| 307 | // overlayable defined with the specified name to be overlaid. |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 308 | return Error("<overlay> android:targetName '%s' does not match overlayable name '%s'", |
| 309 | overlay_info.target_name.c_str(), overlayable_info->name.c_str()); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 312 | // Enforce policy restrictions if the resource is declared as overlayable. |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 313 | if ((overlayable_info->policy_flags & fulfilled_policies) == 0) { |
| 314 | return Error("overlay with policies '%s' does not fulfill any overlayable policies '%s'", |
| 315 | ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(), |
| 316 | ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str()); |
| 317 | } |
| 318 | |
| 319 | return Result<Unit>({}); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 322 | Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const std::string& target_apk_path, |
| 323 | const ApkAssets& target_apk_assets, |
| 324 | const std::string& overlay_apk_path, |
| 325 | const ApkAssets& overlay_apk_assets, |
| 326 | const PolicyBitmask& fulfilled_policies, |
| 327 | bool enforce_overlayable) { |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 328 | SYSTRACE << "Idmap::FromApkAssets"; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 329 | AssetManager2 target_asset_manager; |
| 330 | if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 331 | return Error("failed to create target asset manager"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | AssetManager2 overlay_asset_manager; |
| 335 | if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 336 | return Error("failed to create overlay asset manager"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc(); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 340 | if (target_arsc == nullptr) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 341 | return Error("failed to load target resources.arsc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc(); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 345 | if (overlay_arsc == nullptr) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 346 | return Error("failed to load overlay resources.arsc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 350 | if (target_pkg == nullptr) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 351 | return Error("failed to load target package from resources.arsc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 355 | if (overlay_pkg == nullptr) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 356 | return Error("failed to load overlay package from resources.arsc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path); |
| 360 | if (!target_zip) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 361 | return Error("failed to open target as zip"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path); |
| 365 | if (!overlay_zip) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 366 | return Error("failed to open overlay as zip"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 369 | auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 370 | if (!overlay_info) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 371 | return overlay_info.GetError(); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 374 | std::unique_ptr<IdmapHeader> header(new IdmapHeader()); |
| 375 | header->magic_ = kIdmapMagic; |
| 376 | header->version_ = kIdmapCurrentVersion; |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 377 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 378 | Result<uint32_t> crc = GetCrc(*target_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 379 | if (!crc) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 380 | return Error(crc.GetError(), "failed to get zip CRC for target"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 381 | } |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 382 | header->target_crc_ = *crc; |
| 383 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 384 | crc = GetCrc(*overlay_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 385 | if (!crc) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 386 | return Error(crc.GetError(), "failed to get zip CRC for overlay"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 387 | } |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 388 | header->overlay_crc_ = *crc; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 389 | |
| 390 | if (target_apk_path.size() > sizeof(header->target_path_)) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 391 | return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(), |
| 392 | sizeof(header->target_path_)); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 393 | } |
| 394 | memset(header->target_path_, 0, sizeof(header->target_path_)); |
| 395 | memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size()); |
| 396 | |
| 397 | if (overlay_apk_path.size() > sizeof(header->overlay_path_)) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 398 | return Error("overlay apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(), |
| 399 | sizeof(header->target_path_)); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 400 | } |
| 401 | memset(header->overlay_path_, 0, sizeof(header->overlay_path_)); |
| 402 | memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size()); |
| 403 | |
| 404 | std::unique_ptr<Idmap> idmap(new Idmap()); |
| 405 | idmap->header_ = std::move(header); |
| 406 | |
| 407 | // find the resources that exist in both packages |
| 408 | MatchingResources matching_resources; |
| 409 | const auto end = overlay_pkg->end(); |
| 410 | for (auto iter = overlay_pkg->begin(); iter != end; ++iter) { |
| 411 | const ResourceId overlay_resid = *iter; |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 412 | Result<std::string> name = utils::ResToTypeEntryName(overlay_asset_manager, overlay_resid); |
| 413 | if (!name) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 414 | continue; |
| 415 | } |
| 416 | // prepend "<package>:" to turn name into "<package>:<type>/<name>" |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 417 | const std::string full_name = |
| 418 | base::StringPrintf("%s:%s", target_pkg->GetPackageName().c_str(), name->c_str()); |
| 419 | const ResourceId target_resid = NameToResid(target_asset_manager, full_name); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 420 | if (target_resid == 0) { |
| 421 | continue; |
| 422 | } |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 423 | |
Ryan Mitchell | 2c19bd0 | 2019-03-19 15:14:33 -0700 | [diff] [blame] | 424 | if (enforce_overlayable) { |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 425 | Result<Unit> success = |
| 426 | CheckOverlayable(*target_pkg, *overlay_info, fulfilled_policies, target_resid); |
| 427 | if (!success) { |
| 428 | LOG(WARNING) << "overlay \"" << overlay_apk_path |
| 429 | << "\" is not allowed to overlay resource \"" << full_name |
| 430 | << "\": " << success.GetErrorMessage(); |
| 431 | continue; |
| 432 | } |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 435 | matching_resources.Add(target_resid, overlay_resid); |
| 436 | } |
| 437 | |
Ryan Mitchell | b863ca3 | 2019-03-07 14:31:54 -0800 | [diff] [blame] | 438 | if (matching_resources.Map().empty()) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 439 | return Error("overlay \"%s\" does not successfully overlay any resource", |
| 440 | overlay_apk_path.c_str()); |
Ryan Mitchell | b863ca3 | 2019-03-07 14:31:54 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 443 | // encode idmap data |
| 444 | std::unique_ptr<IdmapData> data(new IdmapData()); |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 445 | const auto types_end = matching_resources.Map().cend(); |
| 446 | for (auto ti = matching_resources.Map().cbegin(); ti != types_end; ++ti) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 447 | auto ei = ti->second.cbegin(); |
| 448 | std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry()); |
| 449 | type->target_type_id_ = EXTRACT_TYPE(ei->first); |
| 450 | type->overlay_type_id_ = EXTRACT_TYPE(ei->second); |
| 451 | type->entry_offset_ = EXTRACT_ENTRY(ei->first); |
| 452 | EntryId last_target_entry = kNoEntry; |
| 453 | for (; ei != ti->second.cend(); ++ei) { |
| 454 | if (last_target_entry != kNoEntry) { |
| 455 | int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1; |
| 456 | type->entries_.insert(type->entries_.end(), count, kNoEntry); |
| 457 | } |
| 458 | type->entries_.push_back(EXTRACT_ENTRY(ei->second)); |
| 459 | last_target_entry = EXTRACT_ENTRY(ei->first); |
| 460 | } |
| 461 | data->type_entries_.push_back(std::move(type)); |
| 462 | } |
| 463 | |
| 464 | std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header()); |
| 465 | data_header->target_package_id_ = target_pkg->GetPackageId(); |
| 466 | data_header->type_count_ = data->type_entries_.size(); |
| 467 | data->header_ = std::move(data_header); |
| 468 | |
| 469 | idmap->data_.push_back(std::move(data)); |
| 470 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 471 | return {std::move(idmap)}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void IdmapHeader::accept(Visitor* v) const { |
| 475 | assert(v != nullptr); |
| 476 | v->visit(*this); |
| 477 | } |
| 478 | |
| 479 | void IdmapData::Header::accept(Visitor* v) const { |
| 480 | assert(v != nullptr); |
| 481 | v->visit(*this); |
| 482 | } |
| 483 | |
| 484 | void IdmapData::TypeEntry::accept(Visitor* v) const { |
| 485 | assert(v != nullptr); |
| 486 | v->visit(*this); |
| 487 | } |
| 488 | |
| 489 | void IdmapData::accept(Visitor* v) const { |
| 490 | assert(v != nullptr); |
| 491 | v->visit(*this); |
| 492 | header_->accept(v); |
| 493 | auto end = type_entries_.cend(); |
| 494 | for (auto iter = type_entries_.cbegin(); iter != end; ++iter) { |
| 495 | (*iter)->accept(v); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | void Idmap::accept(Visitor* v) const { |
| 500 | assert(v != nullptr); |
| 501 | v->visit(*this); |
| 502 | header_->accept(v); |
| 503 | auto end = data_.cend(); |
| 504 | for (auto iter = data_.cbegin(); iter != end; ++iter) { |
| 505 | (*iter)->accept(v); |
| 506 | } |
| 507 | } |
| 508 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 509 | } // namespace android::idmap2 |