Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 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 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 18 | |
| 19 | #include "androidfw/LoadedArsc.h" |
| 20 | |
| 21 | #include <cstddef> |
| 22 | #include <limits> |
| 23 | |
| 24 | #include "android-base/logging.h" |
| 25 | #include "android-base/stringprintf.h" |
| 26 | #include "utils/ByteOrder.h" |
| 27 | #include "utils/Trace.h" |
| 28 | |
| 29 | #ifdef _WIN32 |
| 30 | #ifdef ERROR |
| 31 | #undef ERROR |
| 32 | #endif |
| 33 | #endif |
| 34 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 35 | #include "androidfw/ByteBucketArray.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 36 | #include "androidfw/Chunk.h" |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 37 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 38 | #include "androidfw/Util.h" |
| 39 | |
| 40 | using android::base::StringPrintf; |
| 41 | |
| 42 | namespace android { |
| 43 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 44 | constexpr const static int kAppPackageId = 0x7f; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 45 | |
| 46 | // Element of a TypeSpec array. See TypeSpec. |
| 47 | struct Type { |
| 48 | // The configuration for which this type defines entries. |
| 49 | // This is already converted to host endianness. |
| 50 | ResTable_config configuration; |
| 51 | |
| 52 | // Pointer to the mmapped data where entry definitions are kept. |
| 53 | const ResTable_type* type; |
| 54 | }; |
| 55 | |
| 56 | // TypeSpec is going to be immediately proceeded by |
| 57 | // an array of Type structs, all in the same block of memory. |
| 58 | struct TypeSpec { |
| 59 | // Pointer to the mmapped data where flags are kept. |
| 60 | // Flags denote whether the resource entry is public |
| 61 | // and under which configurations it varies. |
| 62 | const ResTable_typeSpec* type_spec; |
| 63 | |
| 64 | // The number of types that follow this struct. |
| 65 | // There is a type for each configuration |
| 66 | // that entries are defined for. |
| 67 | size_t type_count; |
| 68 | |
| 69 | // Trick to easily access a variable number of Type structs |
| 70 | // proceeding this struct, and to ensure their alignment. |
| 71 | const Type types[0]; |
| 72 | }; |
| 73 | |
| 74 | // TypeSpecPtr points to the block of memory that holds |
| 75 | // a TypeSpec struct, followed by an array of Type structs. |
| 76 | // TypeSpecPtr is a managed pointer that knows how to delete |
| 77 | // itself. |
| 78 | using TypeSpecPtr = util::unique_cptr<TypeSpec>; |
| 79 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 80 | namespace { |
| 81 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 82 | // Builder that helps accumulate Type structs and then create a single |
| 83 | // contiguous block of memory to store both the TypeSpec struct and |
| 84 | // the Type structs. |
| 85 | class TypeSpecPtrBuilder { |
| 86 | public: |
| 87 | TypeSpecPtrBuilder(const ResTable_typeSpec* header) : header_(header) {} |
| 88 | |
| 89 | void AddType(const ResTable_type* type) { |
| 90 | ResTable_config config; |
| 91 | config.copyFromDtoH(type->config); |
| 92 | types_.push_back(Type{config, type}); |
| 93 | } |
| 94 | |
| 95 | TypeSpecPtr Build() { |
| 96 | // Check for overflow. |
| 97 | if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(Type) < types_.size()) { |
| 98 | return {}; |
| 99 | } |
| 100 | TypeSpec* type_spec = (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(Type))); |
| 101 | type_spec->type_spec = header_; |
| 102 | type_spec->type_count = types_.size(); |
| 103 | memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(Type)); |
| 104 | return TypeSpecPtr(type_spec); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder); |
| 109 | |
| 110 | const ResTable_typeSpec* header_; |
| 111 | std::vector<Type> types_; |
| 112 | }; |
| 113 | |
| 114 | } // namespace |
| 115 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 116 | bool LoadedPackage::FindEntry(uint8_t type_idx, uint16_t entry_idx, const ResTable_config& config, |
| 117 | LoadedArscEntry* out_entry, ResTable_config* out_selected_config, |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 118 | uint32_t* out_flags) const { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 119 | ATRACE_CALL(); |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 120 | |
| 121 | // If the type IDs are offset in this package, we need to take that into account when searching |
| 122 | // for a type. |
| 123 | const TypeSpecPtr& ptr = type_specs_[type_idx - type_id_offset_]; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 124 | if (ptr == nullptr) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // Don't bother checking if the entry ID is larger than |
| 129 | // the number of entries. |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 130 | if (entry_idx >= dtohl(ptr->type_spec->entryCount)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
| 134 | const ResTable_config* best_config = nullptr; |
| 135 | const ResTable_type* best_type = nullptr; |
| 136 | uint32_t best_offset = 0; |
| 137 | |
| 138 | for (uint32_t i = 0; i < ptr->type_count; i++) { |
| 139 | const Type* type = &ptr->types[i]; |
| 140 | |
| 141 | if (type->configuration.match(config) && |
| 142 | (best_config == nullptr || type->configuration.isBetterThan(*best_config, &config))) { |
| 143 | // The configuration matches and is better than the previous selection. |
| 144 | // Find the entry value if it exists for this configuration. |
| 145 | size_t entry_count = dtohl(type->type->entryCount); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 146 | if (entry_idx < entry_count) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 147 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
| 148 | reinterpret_cast<const uint8_t*>(type->type) + dtohs(type->type->header.headerSize)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 149 | const uint32_t offset = dtohl(entry_offsets[entry_idx]); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 150 | if (offset != ResTable_type::NO_ENTRY) { |
| 151 | // There is an entry for this resource, record it. |
| 152 | best_config = &type->configuration; |
| 153 | best_type = type->type; |
| 154 | best_offset = offset + dtohl(type->type->entriesStart); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (best_type == nullptr) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | const uint32_t* flags = reinterpret_cast<const uint32_t*>(ptr->type_spec + 1); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 165 | *out_flags = dtohl(flags[entry_idx]); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 166 | *out_selected_config = *best_config; |
| 167 | |
| 168 | const ResTable_entry* best_entry = reinterpret_cast<const ResTable_entry*>( |
| 169 | reinterpret_cast<const uint8_t*>(best_type) + best_offset); |
| 170 | out_entry->entry = best_entry; |
| 171 | out_entry->type_string_ref = StringPoolRef(&type_string_pool_, best_type->id - 1); |
| 172 | out_entry->entry_string_ref = StringPoolRef(&key_string_pool_, dtohl(best_entry->key.index)); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // The destructor gets generated into arbitrary translation units |
| 177 | // if left implicit, which causes the compiler to complain about |
| 178 | // forward declarations and incomplete types. |
| 179 | LoadedArsc::~LoadedArsc() {} |
| 180 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 181 | bool LoadedArsc::FindEntry(uint32_t resid, const ResTable_config& config, |
| 182 | LoadedArscEntry* out_entry, ResTable_config* out_selected_config, |
| 183 | uint32_t* out_flags) const { |
| 184 | ATRACE_CALL(); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 185 | const uint8_t package_id = get_package_id(resid); |
| 186 | const uint8_t type_id = get_type_id(resid); |
| 187 | const uint16_t entry_id = get_entry_id(resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 188 | |
| 189 | if (type_id == 0) { |
| 190 | LOG(ERROR) << "Invalid ID 0x" << std::hex << resid << std::dec << "."; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | for (const auto& loaded_package : packages_) { |
| 195 | if (loaded_package->package_id_ == package_id) { |
| 196 | return loaded_package->FindEntry(type_id - 1, entry_id, config, out_entry, |
| 197 | out_selected_config, out_flags); |
| 198 | } |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 203 | const LoadedPackage* LoadedArsc::GetPackageForId(uint32_t resid) const { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 204 | const uint8_t package_id = get_package_id(resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 205 | for (const auto& loaded_package : packages_) { |
| 206 | if (loaded_package->package_id_ == package_id) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 207 | return loaded_package.get(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | return nullptr; |
| 211 | } |
| 212 | |
| 213 | static bool VerifyType(const Chunk& chunk) { |
| 214 | ATRACE_CALL(); |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 215 | const ResTable_type* header = chunk.header<ResTable_type, kResTableTypeMinSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 216 | |
| 217 | const size_t entry_count = dtohl(header->entryCount); |
| 218 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
| 219 | LOG(ERROR) << "Too many entries in RES_TABLE_TYPE_TYPE."; |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | // Make sure that there is enough room for the entry offsets. |
| 224 | const size_t offsets_offset = chunk.header_size(); |
| 225 | const size_t entries_offset = dtohl(header->entriesStart); |
| 226 | const size_t offsets_length = sizeof(uint32_t) * entry_count; |
| 227 | |
| 228 | if (offsets_offset + offsets_length > entries_offset) { |
| 229 | LOG(ERROR) << "Entry offsets overlap actual entry data."; |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | if (entries_offset > chunk.size()) { |
| 234 | LOG(ERROR) << "Entry offsets extend beyond chunk."; |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if (entries_offset & 0x03) { |
| 239 | LOG(ERROR) << "Entries start at unaligned address."; |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | // Check each entry offset. |
| 244 | const uint32_t* offsets = |
| 245 | reinterpret_cast<const uint32_t*>(reinterpret_cast<const uint8_t*>(header) + offsets_offset); |
| 246 | for (size_t i = 0; i < entry_count; i++) { |
| 247 | uint32_t offset = dtohl(offsets[i]); |
| 248 | if (offset != ResTable_type::NO_ENTRY) { |
| 249 | // Check that the offset is aligned. |
| 250 | if (offset & 0x03) { |
| 251 | LOG(ERROR) << "Entry offset at index " << i << " is not 4-byte aligned."; |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | // Check that the offset doesn't overflow. |
| 256 | if (offset > std::numeric_limits<uint32_t>::max() - entries_offset) { |
| 257 | // Overflow in offset. |
| 258 | LOG(ERROR) << "Entry offset at index " << i << " is too large."; |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | offset += entries_offset; |
| 263 | if (offset > chunk.size() - sizeof(ResTable_entry)) { |
| 264 | LOG(ERROR) << "Entry offset at index " << i << " is too large. No room for ResTable_entry."; |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>( |
| 269 | reinterpret_cast<const uint8_t*>(header) + offset); |
| 270 | const size_t entry_size = dtohs(entry->size); |
| 271 | if (entry_size < sizeof(*entry)) { |
| 272 | LOG(ERROR) << "ResTable_entry size " << entry_size << " is too small."; |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | // Check the declared entrySize. |
| 277 | if (entry_size > chunk.size() || offset > chunk.size() - entry_size) { |
| 278 | LOG(ERROR) << "ResTable_entry size " << entry_size << " is too large."; |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | // If this is a map entry, then keep validating. |
| 283 | if (entry_size >= sizeof(ResTable_map_entry)) { |
| 284 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry); |
| 285 | const size_t map_entry_count = dtohl(map->count); |
| 286 | |
| 287 | size_t map_entries_start = offset + entry_size; |
| 288 | if (map_entries_start & 0x03) { |
| 289 | LOG(ERROR) << "Map entries start at unaligned offset."; |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | // Each entry is sizeof(ResTable_map) big. |
| 294 | if (map_entry_count > ((chunk.size() - map_entries_start) / sizeof(ResTable_map))) { |
| 295 | LOG(ERROR) << "Too many map entries in ResTable_map_entry."; |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | // Great, all the map entries fit!. |
| 300 | } else { |
| 301 | // There needs to be room for one Res_value struct. |
| 302 | if (offset + entry_size > chunk.size() - sizeof(Res_value)) { |
| 303 | LOG(ERROR) << "No room for Res_value after ResTable_entry."; |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | const Res_value* value = reinterpret_cast<const Res_value*>( |
| 308 | reinterpret_cast<const uint8_t*>(entry) + entry_size); |
| 309 | const size_t value_size = dtohs(value->size); |
| 310 | if (value_size < sizeof(Res_value)) { |
| 311 | LOG(ERROR) << "Res_value is too small."; |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | if (value_size > chunk.size() || offset + entry_size > chunk.size() - value_size) { |
| 316 | LOG(ERROR) << "Res_value size is too large."; |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 325 | void LoadedPackage::CollectConfigurations(bool exclude_mipmap, |
| 326 | std::set<ResTable_config>* out_configs) const { |
| 327 | const static std::u16string kMipMap = u"mipmap"; |
| 328 | const size_t type_count = type_specs_.size(); |
| 329 | for (size_t i = 0; i < type_count; i++) { |
| 330 | const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i]; |
| 331 | if (type_spec != nullptr) { |
| 332 | if (exclude_mipmap) { |
| 333 | const int type_idx = type_spec->type_spec->id - 1; |
| 334 | size_t type_name_len; |
| 335 | const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len); |
| 336 | if (type_name16 != nullptr) { |
| 337 | if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) { |
| 338 | // This is a mipmap type, skip collection. |
| 339 | continue; |
| 340 | } |
| 341 | } |
| 342 | const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len); |
| 343 | if (type_name != nullptr) { |
| 344 | if (strncmp(type_name, "mipmap", type_name_len) == 0) { |
| 345 | // This is a mipmap type, skip collection. |
| 346 | continue; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | for (size_t j = 0; j < type_spec->type_count; j++) { |
| 352 | out_configs->insert(type_spec->types[j].configuration); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const { |
| 359 | char temp_locale[RESTABLE_MAX_LOCALE_LEN]; |
| 360 | const size_t type_count = type_specs_.size(); |
| 361 | for (size_t i = 0; i < type_count; i++) { |
| 362 | const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i]; |
| 363 | if (type_spec != nullptr) { |
| 364 | for (size_t j = 0; j < type_spec->type_count; j++) { |
| 365 | const ResTable_config& configuration = type_spec->types[j].configuration; |
| 366 | if (configuration.locale != 0) { |
| 367 | configuration.getBcp47Locale(temp_locale, canonicalize); |
| 368 | std::string locale(temp_locale); |
| 369 | out_locales->insert(std::move(locale)); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 376 | uint32_t LoadedPackage::FindEntryByName(const std::u16string& type_name, |
| 377 | const std::u16string& entry_name) const { |
| 378 | ssize_t type_idx = type_string_pool_.indexOfString(type_name.data(), type_name.size()); |
| 379 | if (type_idx < 0) { |
| 380 | return 0u; |
| 381 | } |
| 382 | |
| 383 | ssize_t key_idx = key_string_pool_.indexOfString(entry_name.data(), entry_name.size()); |
| 384 | if (key_idx < 0) { |
| 385 | return 0u; |
| 386 | } |
| 387 | |
| 388 | const TypeSpec* type_spec = type_specs_[type_idx].get(); |
| 389 | if (type_spec == nullptr) { |
| 390 | return 0u; |
| 391 | } |
| 392 | |
| 393 | for (size_t ti = 0; ti < type_spec->type_count; ti++) { |
| 394 | const Type* type = &type_spec->types[ti]; |
| 395 | size_t entry_count = dtohl(type->type->entryCount); |
| 396 | for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) { |
| 397 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
| 398 | reinterpret_cast<const uint8_t*>(type->type) + dtohs(type->type->header.headerSize)); |
| 399 | const uint32_t offset = dtohl(entry_offsets[entry_idx]); |
| 400 | if (offset != ResTable_type::NO_ENTRY) { |
| 401 | const ResTable_entry* entry = |
| 402 | reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type->type) + |
| 403 | dtohl(type->type->entriesStart) + offset); |
| 404 | if (dtohl(entry->key.index) == static_cast<uint32_t>(key_idx)) { |
| 405 | // The package ID will be overridden by the caller (due to runtime assignment of package |
| 406 | // IDs for shared libraries). |
| 407 | return make_resid(0x00, type_idx + type_id_offset_ + 1, entry_idx); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | return 0u; |
| 413 | } |
| 414 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 415 | std::unique_ptr<LoadedPackage> LoadedPackage::Load(const Chunk& chunk) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 416 | ATRACE_CALL(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 417 | std::unique_ptr<LoadedPackage> loaded_package{new LoadedPackage()}; |
| 418 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 419 | const ResTable_package* header = chunk.header<ResTable_package>(); |
| 420 | if (header == nullptr) { |
| 421 | LOG(ERROR) << "Chunk RES_TABLE_PACKAGE_TYPE is too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 422 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | loaded_package->package_id_ = dtohl(header->id); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 426 | if (loaded_package->package_id_ == 0) { |
| 427 | // Package ID of 0 means this is a shared library. |
| 428 | loaded_package->dynamic_ = true; |
| 429 | } |
| 430 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 431 | if (header->header.headerSize >= sizeof(ResTable_package)) { |
| 432 | uint32_t type_id_offset = dtohl(header->typeIdOffset); |
| 433 | if (type_id_offset > std::numeric_limits<uint8_t>::max()) { |
| 434 | LOG(ERROR) << "Type ID offset in RES_TABLE_PACKAGE_TYPE is too large."; |
| 435 | return {}; |
| 436 | } |
| 437 | loaded_package->type_id_offset_ = static_cast<int>(type_id_offset); |
| 438 | } |
| 439 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 440 | util::ReadUtf16StringFromDevice(header->name, arraysize(header->name), |
| 441 | &loaded_package->package_name_); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 442 | |
| 443 | // A TypeSpec builder. We use this to accumulate the set of Types |
| 444 | // available for a TypeSpec, and later build a single, contiguous block |
| 445 | // of memory that holds all the Types together with the TypeSpec. |
| 446 | std::unique_ptr<TypeSpecPtrBuilder> types_builder; |
| 447 | |
| 448 | // Keep track of the last seen type index. Since type IDs are 1-based, |
| 449 | // this records their index, which is 0-based (type ID - 1). |
| 450 | uint8_t last_type_idx = 0; |
| 451 | |
| 452 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 453 | while (iter.HasNext()) { |
| 454 | const Chunk child_chunk = iter.Next(); |
| 455 | switch (child_chunk.type()) { |
| 456 | case RES_STRING_POOL_TYPE: { |
| 457 | const uintptr_t pool_address = |
| 458 | reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>()); |
| 459 | const uintptr_t header_address = reinterpret_cast<uintptr_t>(header); |
| 460 | if (pool_address == header_address + dtohl(header->typeStrings)) { |
| 461 | // This string pool is the type string pool. |
| 462 | status_t err = loaded_package->type_string_pool_.setTo( |
| 463 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 464 | if (err != NO_ERROR) { |
| 465 | LOG(ERROR) << "Corrupt package type string pool."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 466 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 467 | } |
| 468 | } else if (pool_address == header_address + dtohl(header->keyStrings)) { |
| 469 | // This string pool is the key string pool. |
| 470 | status_t err = loaded_package->key_string_pool_.setTo( |
| 471 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 472 | if (err != NO_ERROR) { |
| 473 | LOG(ERROR) << "Corrupt package key string pool."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 474 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 475 | } |
| 476 | } else { |
| 477 | LOG(WARNING) << "Too many string pool chunks found in package."; |
| 478 | } |
| 479 | } break; |
| 480 | |
| 481 | case RES_TABLE_TYPE_SPEC_TYPE: { |
| 482 | ATRACE_NAME("LoadTableTypeSpec"); |
| 483 | |
| 484 | // Starting a new TypeSpec, so finish the old one if there was one. |
| 485 | if (types_builder) { |
| 486 | TypeSpecPtr type_spec_ptr = types_builder->Build(); |
| 487 | if (type_spec_ptr == nullptr) { |
| 488 | LOG(ERROR) << "Too many type configurations, overflow detected."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 489 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 490 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 491 | loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr); |
| 492 | |
| 493 | types_builder = {}; |
| 494 | last_type_idx = 0; |
| 495 | } |
| 496 | |
| 497 | const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>(); |
| 498 | if (type_spec == nullptr) { |
| 499 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE is too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 500 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (type_spec->id == 0) { |
| 504 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 505 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 508 | if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) > |
| 509 | std::numeric_limits<uint8_t>::max()) { |
| 510 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has out of range ID."; |
| 511 | return {}; |
| 512 | } |
| 513 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 514 | // The data portion of this chunk contains entry_count 32bit entries, |
| 515 | // each one representing a set of flags. |
| 516 | // Here we only validate that the chunk is well formed. |
| 517 | const size_t entry_count = dtohl(type_spec->entryCount); |
| 518 | |
| 519 | // There can only be 2^16 entries in a type, because that is the ID |
| 520 | // space for entries (EEEE) in the resource ID 0xPPTTEEEE. |
| 521 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
| 522 | LOG(ERROR) << "Too many entries in RES_TABLE_TYPE_SPEC_TYPE: " << entry_count << "."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 523 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | if (entry_count * sizeof(uint32_t) > chunk.data_size()) { |
| 527 | LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_TYPE_SPEC_TYPE."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 528 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | last_type_idx = type_spec->id - 1; |
| 532 | types_builder = util::make_unique<TypeSpecPtrBuilder>(type_spec); |
| 533 | } break; |
| 534 | |
| 535 | case RES_TABLE_TYPE_TYPE: { |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 536 | const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 537 | if (type == nullptr) { |
| 538 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE is too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 539 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | if (type->id == 0) { |
| 543 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE has invalid ID 0."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 544 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // Type chunks must be preceded by their TypeSpec chunks. |
| 548 | if (!types_builder || type->id - 1 != last_type_idx) { |
| 549 | LOG(ERROR) << "Found RES_TABLE_TYPE_TYPE chunk without " |
| 550 | "RES_TABLE_TYPE_SPEC_TYPE."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 551 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if (!VerifyType(child_chunk)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 555 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | types_builder->AddType(type); |
| 559 | } break; |
| 560 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 561 | case RES_TABLE_LIBRARY_TYPE: { |
| 562 | const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>(); |
| 563 | if (lib == nullptr) { |
| 564 | LOG(ERROR) << "Chunk RES_TABLE_LIBRARY_TYPE is too small."; |
| 565 | return {}; |
| 566 | } |
| 567 | |
| 568 | if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) { |
| 569 | LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_LIBRARY_TYPE."; |
| 570 | return {}; |
| 571 | } |
| 572 | |
| 573 | loaded_package->dynamic_package_map_.reserve(dtohl(lib->count)); |
| 574 | |
| 575 | const ResTable_lib_entry* const entry_begin = |
| 576 | reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr()); |
| 577 | const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count); |
| 578 | for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) { |
| 579 | std::string package_name; |
| 580 | util::ReadUtf16StringFromDevice(entry_iter->packageName, |
| 581 | arraysize(entry_iter->packageName), &package_name); |
| 582 | |
| 583 | if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) { |
| 584 | LOG(ERROR) << base::StringPrintf( |
| 585 | "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.", |
| 586 | dtohl(entry_iter->packageId), package_name.c_str()); |
| 587 | return {}; |
| 588 | } |
| 589 | |
| 590 | loaded_package->dynamic_package_map_.emplace_back(std::move(package_name), |
| 591 | dtohl(entry_iter->packageId)); |
| 592 | } |
| 593 | |
| 594 | } break; |
| 595 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 596 | default: |
| 597 | LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | // Finish the last TypeSpec. |
| 603 | if (types_builder) { |
| 604 | TypeSpecPtr type_spec_ptr = types_builder->Build(); |
| 605 | if (type_spec_ptr == nullptr) { |
| 606 | LOG(ERROR) << "Too many type configurations, overflow detected."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 607 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 608 | } |
| 609 | loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr); |
| 610 | } |
| 611 | |
| 612 | if (iter.HadError()) { |
| 613 | LOG(ERROR) << iter.GetLastError(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 614 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 615 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 616 | return loaded_package; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 619 | bool LoadedArsc::LoadTable(const Chunk& chunk, bool load_as_shared_library) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 620 | ATRACE_CALL(); |
| 621 | const ResTable_header* header = chunk.header<ResTable_header>(); |
| 622 | if (header == nullptr) { |
| 623 | LOG(ERROR) << "Chunk RES_TABLE_TYPE is too small."; |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | const size_t package_count = dtohl(header->packageCount); |
| 628 | size_t packages_seen = 0; |
| 629 | |
| 630 | packages_.reserve(package_count); |
| 631 | |
| 632 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 633 | while (iter.HasNext()) { |
| 634 | const Chunk child_chunk = iter.Next(); |
| 635 | switch (child_chunk.type()) { |
| 636 | case RES_STRING_POOL_TYPE: |
| 637 | // Only use the first string pool. Ignore others. |
| 638 | if (global_string_pool_.getError() == NO_INIT) { |
| 639 | status_t err = global_string_pool_.setTo(child_chunk.header<ResStringPool_header>(), |
| 640 | child_chunk.size()); |
| 641 | if (err != NO_ERROR) { |
| 642 | LOG(ERROR) << "Corrupt string pool."; |
| 643 | return false; |
| 644 | } |
| 645 | } else { |
| 646 | LOG(WARNING) << "Multiple string pool chunks found in resource table."; |
| 647 | } |
| 648 | break; |
| 649 | |
| 650 | case RES_TABLE_PACKAGE_TYPE: { |
| 651 | if (packages_seen + 1 > package_count) { |
| 652 | LOG(ERROR) << "More package chunks were found than the " << package_count |
| 653 | << " declared in the " |
| 654 | "header."; |
| 655 | return false; |
| 656 | } |
| 657 | packages_seen++; |
| 658 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 659 | std::unique_ptr<LoadedPackage> loaded_package = LoadedPackage::Load(child_chunk); |
| 660 | if (!loaded_package) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 661 | return false; |
| 662 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 663 | |
| 664 | // Mark the package as dynamic if we are forcefully loading the Apk as a shared library. |
| 665 | if (loaded_package->package_id_ == kAppPackageId) { |
| 666 | loaded_package->dynamic_ = load_as_shared_library; |
| 667 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 668 | loaded_package->system_ = system_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 669 | packages_.push_back(std::move(loaded_package)); |
| 670 | } break; |
| 671 | |
| 672 | default: |
| 673 | LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (iter.HadError()) { |
| 679 | LOG(ERROR) << iter.GetLastError(); |
| 680 | return false; |
| 681 | } |
| 682 | return true; |
| 683 | } |
| 684 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 685 | std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const void* data, size_t len, bool system, |
| 686 | bool load_as_shared_library) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 687 | ATRACE_CALL(); |
| 688 | |
| 689 | // Not using make_unique because the constructor is private. |
| 690 | std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc()); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 691 | loaded_arsc->system_ = system; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 692 | |
| 693 | ChunkIterator iter(data, len); |
| 694 | while (iter.HasNext()) { |
| 695 | const Chunk chunk = iter.Next(); |
| 696 | switch (chunk.type()) { |
| 697 | case RES_TABLE_TYPE: |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 698 | if (!loaded_arsc->LoadTable(chunk, load_as_shared_library)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 699 | return {}; |
| 700 | } |
| 701 | break; |
| 702 | |
| 703 | default: |
| 704 | LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (iter.HadError()) { |
| 710 | LOG(ERROR) << iter.GetLastError(); |
| 711 | return {}; |
| 712 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 713 | |
| 714 | // Need to force a move for mingw32. |
| 715 | return std::move(loaded_arsc); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | } // namespace android |