blob: 2533f8079c39fb7939d704c56189d0a17500aee5 [file] [log] [blame]
Mohamed Heikalc7694032018-11-07 16:49:02 -05001/*
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
felkachang4bdd3ac2022-09-13 10:58:49 +080017#include "optimize/Obfuscator.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050018
felkachang8ceb39c2022-09-13 10:58:49 +080019#include <map>
Mohamed Heikalb2bac122019-07-17 17:47:17 -040020#include <set>
felkachang4bdd3ac2022-09-13 10:58:49 +080021#include <string>
Mohamed Heikalc7694032018-11-07 16:49:02 -050022#include <unordered_set>
23
Mohamed Heikalc7694032018-11-07 16:49:02 -050024#include "ResourceTable.h"
25#include "ValueVisitor.h"
felkachang4bdd3ac2022-09-13 10:58:49 +080026#include "androidfw/StringPiece.h"
Mohamed Heikal61d528f2019-07-08 15:06:39 -040027#include "util/Util.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050028
felkachang4bdd3ac2022-09-13 10:58:49 +080029static const char base64_chars[] =
30 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31 "abcdefghijklmnopqrstuvwxyz"
32 "0123456789-_";
Mohamed Heikalc7694032018-11-07 16:49:02 -050033
34namespace aapt {
35
felkachang8ceb39c2022-09-13 10:58:49 +080036Obfuscator::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 Heikalc7694032018-11-07 16:49:02 -050040}
41
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070042std::string ShortenFileName(android::StringPiece file_path, int output_length) {
Mohamed Heikalc7694032018-11-07 16:49:02 -050043 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 Heikal525714a2019-07-18 11:14:31 -040054// Return the optimal hash length such that at most 10% of resources collide in
55// their shortened path.
Mohamed Heikalc7694032018-11-07 16:49:02 -050056// Reference: http://matt.might.net/articles/counting-hash-collisions/
57int OptimalShortenedLength(int num_resources) {
Mohamed Heikal525714a2019-07-18 11:14:31 -040058 if (num_resources > 4000) {
59 return 3;
60 } else {
61 return 2;
Mohamed Heikalc7694032018-11-07 16:49:02 -050062 }
Mohamed Heikalc7694032018-11-07 16:49:02 -050063}
64
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070065std::string GetShortenedPath(android::StringPiece shortened_filename,
66 android::StringPiece extension, int collision_count) {
67 std::string shortened_path = std::string("res/") += shortened_filename;
Mohamed Heikalc7694032018-11-07 16:49:02 -050068 if (collision_count > 0) {
69 shortened_path += std::to_string(collision_count);
70 }
71 shortened_path += extension;
72 return shortened_path;
73}
74
Mohamed Heikalb2bac122019-07-17 17:47:17 -040075// 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.
78struct PathComparator {
felkachang4bdd3ac2022-09-13 10:58:49 +080079 bool operator()(const FileReference* lhs, const FileReference* rhs) const {
80 return lhs->path->compare(*rhs->path);
81 }
Mohamed Heikalb2bac122019-07-17 17:47:17 -040082};
83
felkachang8ceb39c2022-09-13 10:58:49 +080084static bool HandleShortenFilePaths(ResourceTable* table,
85 std::map<std::string, std::string>& shortened_path_map) {
Mohamed Heikalc7694032018-11-07 16:49:02 -050086 // used to detect collisions
87 std::unordered_set<std::string> shortened_paths;
Mohamed Heikalb2bac122019-07-17 17:47:17 -040088 std::set<FileReference*, PathComparator> file_refs;
Mohamed Heikalc7694032018-11-07 16:49:02 -050089 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 Heikal61d528f2019-07-08 15:06:39 -0400106 // Android detects ColorStateLists via pathname, skip res/color*
felkachang4bdd3ac2022-09-13 10:58:49 +0800107 if (util::StartsWith(res_subdir, "res/color")) continue;
Mohamed Heikal7c757302019-04-25 17:39:43 -0400108
Mohamed Heikalc7694032018-11-07 16:49:02 -0500109 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);
felkachang8ceb39c2022-09-13 10:58:49 +0800117 shortened_path_map.insert({*file_ref->path, shortened_path});
Mohamed Heikalc7694032018-11-07 16:49:02 -0500118 file_ref->path = table->string_pool.MakeRef(shortened_path, file_ref->path.GetContext());
119 }
120 return true;
121}
122
felkachang8ceb39c2022-09-13 10:58:49 +0800123void 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
146static 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
186bool 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 */
203bool Obfuscator::IsEnabled() const {
204 return shorten_resource_paths_ || collapse_key_stringpool_;
205}
206
Mohamed Heikalc7694032018-11-07 16:49:02 -0500207} // namespace aapt