Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [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 | |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 17 | #include "optimize/Obfuscator.h" |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 18 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 19 | #include <map> |
Mohamed Heikal | b2bac12 | 2019-07-17 17:47:17 -0400 | [diff] [blame] | 20 | #include <set> |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 21 | #include <string> |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 22 | #include <unordered_set> |
| 23 | |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 24 | #include "ResourceTable.h" |
| 25 | #include "ValueVisitor.h" |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 26 | #include "androidfw/StringPiece.h" |
Mohamed Heikal | 61d528f | 2019-07-08 15:06:39 -0400 | [diff] [blame] | 27 | #include "util/Util.h" |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 28 | |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 29 | static const char base64_chars[] = |
| 30 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 31 | "abcdefghijklmnopqrstuvwxyz" |
| 32 | "0123456789-_"; |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 33 | |
| 34 | namespace aapt { |
| 35 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 36 | Obfuscator::Obfuscator(OptimizeOptions& optimizeOptions) |
| 37 | : options_(optimizeOptions.table_flattener_options), |
| 38 | shorten_resource_paths_(optimizeOptions.shorten_resource_paths), |
| 39 | collapse_key_stringpool_(optimizeOptions.table_flattener_options.collapse_key_stringpool) { |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | std::string ShortenFileName(const android::StringPiece& file_path, int output_length) { |
| 43 | std::size_t hash_num = std::hash<android::StringPiece>{}(file_path); |
| 44 | std::string result = ""; |
| 45 | // Convert to (modified) base64 so that it is a proper file path. |
| 46 | for (int i = 0; i < output_length; i++) { |
| 47 | uint8_t sextet = hash_num & 0x3f; |
| 48 | hash_num >>= 6; |
| 49 | result += base64_chars[sextet]; |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 | |
Mohamed Heikal | 525714a | 2019-07-18 11:14:31 -0400 | [diff] [blame] | 54 | // Return the optimal hash length such that at most 10% of resources collide in |
| 55 | // their shortened path. |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 56 | // Reference: http://matt.might.net/articles/counting-hash-collisions/ |
| 57 | int OptimalShortenedLength(int num_resources) { |
Mohamed Heikal | 525714a | 2019-07-18 11:14:31 -0400 | [diff] [blame] | 58 | if (num_resources > 4000) { |
| 59 | return 3; |
| 60 | } else { |
| 61 | return 2; |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 62 | } |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | std::string GetShortenedPath(const android::StringPiece& shortened_filename, |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 66 | const android::StringPiece& extension, int collision_count) { |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 67 | std::string shortened_path = "res/" + shortened_filename.to_string(); |
| 68 | if (collision_count > 0) { |
| 69 | shortened_path += std::to_string(collision_count); |
| 70 | } |
| 71 | shortened_path += extension; |
| 72 | return shortened_path; |
| 73 | } |
| 74 | |
Mohamed Heikal | b2bac12 | 2019-07-17 17:47:17 -0400 | [diff] [blame] | 75 | // implement custom comparator of FileReference pointers so as to use the |
| 76 | // underlying filepath as key rather than the integer address. This is to ensure |
| 77 | // determinism of output for colliding files. |
| 78 | struct PathComparator { |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 79 | bool operator()(const FileReference* lhs, const FileReference* rhs) const { |
| 80 | return lhs->path->compare(*rhs->path); |
| 81 | } |
Mohamed Heikal | b2bac12 | 2019-07-17 17:47:17 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 84 | static bool HandleShortenFilePaths(ResourceTable* table, |
| 85 | std::map<std::string, std::string>& shortened_path_map) { |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 86 | // used to detect collisions |
| 87 | std::unordered_set<std::string> shortened_paths; |
Mohamed Heikal | b2bac12 | 2019-07-17 17:47:17 -0400 | [diff] [blame] | 88 | std::set<FileReference*, PathComparator> file_refs; |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 89 | for (auto& package : table->packages) { |
| 90 | for (auto& type : package->types) { |
| 91 | for (auto& entry : type->entries) { |
| 92 | for (auto& config_value : entry->values) { |
| 93 | FileReference* file_ref = ValueCast<FileReference>(config_value->value.get()); |
| 94 | if (file_ref) { |
| 95 | file_refs.insert(file_ref); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | int num_chars = OptimalShortenedLength(file_refs.size()); |
| 102 | for (auto& file_ref : file_refs) { |
| 103 | android::StringPiece res_subdir, actual_filename, extension; |
| 104 | util::ExtractResFilePathParts(*file_ref->path, &res_subdir, &actual_filename, &extension); |
| 105 | |
Mohamed Heikal | 61d528f | 2019-07-08 15:06:39 -0400 | [diff] [blame] | 106 | // Android detects ColorStateLists via pathname, skip res/color* |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 107 | if (util::StartsWith(res_subdir, "res/color")) continue; |
Mohamed Heikal | 7c75730 | 2019-04-25 17:39:43 -0400 | [diff] [blame] | 108 | |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 109 | std::string shortened_filename = ShortenFileName(*file_ref->path, num_chars); |
| 110 | int collision_count = 0; |
| 111 | std::string shortened_path = GetShortenedPath(shortened_filename, extension, collision_count); |
| 112 | while (shortened_paths.find(shortened_path) != shortened_paths.end()) { |
| 113 | collision_count++; |
| 114 | shortened_path = GetShortenedPath(shortened_filename, extension, collision_count); |
| 115 | } |
| 116 | shortened_paths.insert(shortened_path); |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 117 | shortened_path_map.insert({*file_ref->path, shortened_path}); |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 118 | file_ref->path = table->string_pool.MakeRef(shortened_path, file_ref->path.GetContext()); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 123 | void Obfuscator::ObfuscateResourceName( |
| 124 | const bool collapse_key_stringpool, const std::set<ResourceName>& name_collapse_exemptions, |
| 125 | const ResourceNamedType& type_name, const ResourceTableEntryView& entry, |
| 126 | const android::base::function_ref<void(Result obfuscatedResult, const ResourceName&)> |
| 127 | onObfuscate) { |
| 128 | ResourceName resource_name({}, type_name, entry.name); |
| 129 | if (!collapse_key_stringpool || |
| 130 | name_collapse_exemptions.find(resource_name) != name_collapse_exemptions.end()) { |
| 131 | onObfuscate(Result::Keep_ExemptionList, resource_name); |
| 132 | } else { |
| 133 | // resource isn't exempt from collapse, add it as obfuscated value |
| 134 | if (entry.overlayable_item) { |
| 135 | // if the resource name of the specific entry is obfuscated and this |
| 136 | // entry is in the overlayable list, the overlay can't work on this |
| 137 | // overlayable at runtime because the name has been obfuscated in |
| 138 | // resources.arsc during flatten operation. |
| 139 | onObfuscate(Result::Keep_Overlayable, resource_name); |
| 140 | } else { |
| 141 | onObfuscate(Result::Obfuscated, resource_name); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static bool HandleCollapseKeyStringPool( |
| 147 | const ResourceTable* table, const bool collapse_key_string_pool, |
| 148 | const std::set<ResourceName>& name_collapse_exemptions, |
| 149 | std::unordered_map<uint32_t, std::string>& id_resource_map) { |
| 150 | if (!collapse_key_string_pool) { |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | int entryResId = 0; |
| 155 | auto onObfuscate = [&entryResId, &id_resource_map](const Obfuscator::Result obfuscatedResult, |
| 156 | const ResourceName& resource_name) { |
| 157 | if (obfuscatedResult == Obfuscator::Result::Obfuscated) { |
| 158 | id_resource_map.insert({entryResId, resource_name.entry}); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | for (auto& package : table->packages) { |
| 163 | for (auto& type : package->types) { |
| 164 | for (auto& entry : type->entries) { |
| 165 | if (!entry->id.has_value() || entry->name.empty()) { |
| 166 | continue; |
| 167 | } |
| 168 | entryResId = entry->id->id; |
| 169 | ResourceTableEntryView entry_view{ |
| 170 | .name = entry->name, |
| 171 | .id = entry->id ? entry->id.value().entry_id() : (std::optional<uint16_t>)std::nullopt, |
| 172 | .visibility = entry->visibility, |
| 173 | .allow_new = entry->allow_new, |
| 174 | .overlayable_item = entry->overlayable_item, |
| 175 | .staged_id = entry->staged_id}; |
| 176 | |
| 177 | Obfuscator::ObfuscateResourceName(collapse_key_string_pool, name_collapse_exemptions, |
| 178 | type->named_type, entry_view, onObfuscate); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool Obfuscator::Consume(IAaptContext* context, ResourceTable* table) { |
| 187 | HandleCollapseKeyStringPool(table, options_.collapse_key_stringpool, |
| 188 | options_.name_collapse_exemptions, options_.id_resource_map); |
| 189 | if (shorten_resource_paths_) { |
| 190 | return HandleShortenFilePaths(table, options_.shortened_path_map); |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Tell the optimizer whether it's needed to dump information for de-obfuscating. |
| 197 | * |
| 198 | * There are two conditions need to dump the information for de-obfuscating. |
| 199 | * * the option of shortening file paths is enabled. |
| 200 | * * the option of collapsing resource names is enabled. |
| 201 | * @return true if the information needed for de-obfuscating, otherwise false |
| 202 | */ |
| 203 | bool Obfuscator::IsEnabled() const { |
| 204 | return shorten_resource_paths_ || collapse_key_stringpool_; |
| 205 | } |
| 206 | |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 207 | } // namespace aapt |