1 /*
2 * Copyright (C) 2021 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 "idmap2/FabricatedOverlay.h"
18
19 #include <sys/stat.h> // umask
20 #include <sys/types.h> // umask
21
22 #include <android-base/file.h>
23 #include <androidfw/ResourceUtils.h>
24 #include <androidfw/StringPool.h>
25 #include <google/protobuf/io/coded_stream.h>
26 #include <google/protobuf/io/zero_copy_stream_impl.h>
27 #include <utils/ByteOrder.h>
28 #include <zlib.h>
29
30 #include <fstream>
31 #include <map>
32 #include <memory>
33 #include <string>
34 #include <utility>
35
36 namespace android::idmap2 {
37
38 constexpr auto kBufferSize = 1024;
39
40 namespace {
Read32(std::istream & stream,uint32_t * out)41 bool Read32(std::istream& stream, uint32_t* out) {
42 uint32_t value;
43 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
44 *out = dtohl(value);
45 return true;
46 }
47 return false;
48 }
49
Write32(std::ostream & stream,uint32_t value)50 void Write32(std::ostream& stream, uint32_t value) {
51 uint32_t x = htodl(value);
52 stream.write(reinterpret_cast<char*>(&x), sizeof(uint32_t));
53 }
54 } // namespace
55
FabricatedOverlay(pb::FabricatedOverlay && overlay,std::string && string_pool_data,std::vector<android::base::borrowed_fd> binary_files,off_t total_binary_bytes,std::optional<uint32_t> crc_from_disk)56 FabricatedOverlay::FabricatedOverlay(pb::FabricatedOverlay&& overlay,
57 std::string&& string_pool_data,
58 std::vector<android::base::borrowed_fd> binary_files,
59 off_t total_binary_bytes,
60 std::optional<uint32_t> crc_from_disk)
61 : overlay_pb_(std::forward<pb::FabricatedOverlay>(overlay)),
62 string_pool_data_(std::move(string_pool_data)),
63 binary_files_(std::move(binary_files)),
64 total_binary_bytes_(total_binary_bytes),
65 crc_from_disk_(crc_from_disk) {
66 }
67
Builder(const std::string & package_name,const std::string & name,const std::string & target_package_name)68 FabricatedOverlay::Builder::Builder(const std::string& package_name, const std::string& name,
69 const std::string& target_package_name) {
70 package_name_ = package_name;
71 name_ = name;
72 target_package_name_ = target_package_name;
73 }
74
SetOverlayable(const std::string & name)75 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetOverlayable(const std::string& name) {
76 target_overlayable_ = name;
77 return *this;
78 }
79
SetResourceValue(const std::string & resource_name,uint8_t data_type,uint32_t data_value,const std::string & configuration)80 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue(
81 const std::string& resource_name, uint8_t data_type, uint32_t data_value,
82 const std::string& configuration) {
83 entries_.emplace_back(
84 Entry{resource_name, data_type, data_value, "", std::nullopt, configuration});
85 return *this;
86 }
87
SetResourceValue(const std::string & resource_name,uint8_t data_type,const std::string & data_string_value,const std::string & configuration)88 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue(
89 const std::string& resource_name, uint8_t data_type, const std::string& data_string_value,
90 const std::string& configuration) {
91 entries_.emplace_back(
92 Entry{resource_name, data_type, 0, data_string_value, std::nullopt, configuration});
93 return *this;
94 }
95
SetResourceValue(const std::string & resource_name,std::optional<android::base::borrowed_fd> && binary_value,const std::string & configuration)96 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue(
97 const std::string& resource_name, std::optional<android::base::borrowed_fd>&& binary_value,
98 const std::string& configuration) {
99 entries_.emplace_back(Entry{resource_name, 0, 0, "", binary_value, configuration});
100 return *this;
101 }
102
Build()103 Result<FabricatedOverlay> FabricatedOverlay::Builder::Build() {
104 using ConfigMap = std::map<std::string, TargetValue, std::less<>>;
105 using EntryMap = std::map<std::string, ConfigMap, std::less<>>;
106 using TypeMap = std::map<std::string, EntryMap, std::less<>>;
107 using PackageMap = std::map<std::string, TypeMap, std::less<>>;
108 PackageMap package_map;
109 android::StringPool string_pool;
110 for (const auto& res_entry : entries_) {
111 StringPiece package_substr;
112 StringPiece type_name;
113 StringPiece entry_name;
114 if (!android::ExtractResourceName(StringPiece(res_entry.resource_name), &package_substr,
115 &type_name, &entry_name)) {
116 return Error("failed to parse resource name '%s'", res_entry.resource_name.c_str());
117 }
118
119 std::string_view package_name = package_substr.empty() ? target_package_name_ : package_substr;
120 if (type_name.empty()) {
121 return Error("resource name '%s' missing type name", res_entry.resource_name.c_str());
122 }
123
124 if (entry_name.empty()) {
125 return Error("resource name '%s' missing entry name", res_entry.resource_name.c_str());
126 }
127
128 auto package = package_map.find(package_name);
129 if (package == package_map.end()) {
130 package = package_map
131 .insert(std::make_pair(package_name, TypeMap()))
132 .first;
133 }
134
135 auto type = package->second.find(type_name);
136 if (type == package->second.end()) {
137 type = package->second.insert(std::make_pair(type_name, EntryMap())).first;
138 }
139
140 auto entry = type->second.find(entry_name);
141 if (entry == type->second.end()) {
142 entry = type->second.insert(std::make_pair(entry_name, ConfigMap())).first;
143 }
144
145 auto value = entry->second.find(res_entry.configuration);
146 if (value == entry->second.end()) {
147 value = entry->second.insert(std::make_pair(res_entry.configuration, TargetValue())).first;
148 }
149
150 value->second = TargetValue{res_entry.data_type, res_entry.data_value,
151 res_entry.data_string_value, res_entry.data_binary_value};
152 }
153
154 pb::FabricatedOverlay overlay_pb;
155 overlay_pb.set_package_name(package_name_);
156 overlay_pb.set_name(name_);
157 overlay_pb.set_target_package_name(target_package_name_);
158 overlay_pb.set_target_overlayable(target_overlayable_);
159
160 std::vector<android::base::borrowed_fd> binary_files;
161 size_t total_binary_bytes = 0;
162 // 16 for the number of bytes in the frro file before the binary data
163 const size_t FRRO_HEADER_SIZE = 16;
164
165 for (auto& package : package_map) {
166 auto package_pb = overlay_pb.add_packages();
167 package_pb->set_name(package.first);
168
169 for (auto& type : package.second) {
170 auto type_pb = package_pb->add_types();
171 type_pb->set_name(type.first);
172
173 for (auto& entry : type.second) {
174 for (const auto& value: entry.second) {
175 auto entry_pb = type_pb->add_entries();
176 entry_pb->set_name(entry.first);
177 entry_pb->set_configuration(value.first);
178 pb::ResourceValue* pb_value = entry_pb->mutable_res_value();
179 pb_value->set_data_type(value.second.data_type);
180 if (value.second.data_type == Res_value::TYPE_STRING) {
181 auto ref = string_pool.MakeRef(value.second.data_string_value);
182 pb_value->set_data_value(ref.index());
183 } else if (value.second.data_binary_value.has_value()) {
184 pb_value->set_data_type(Res_value::TYPE_STRING);
185 struct stat s;
186 if (fstat(value.second.data_binary_value->get(), &s) == -1) {
187 return Error("unable to get size of binary file: %d", errno);
188 }
189 std::string uri
190 = StringPrintf("frro:/%s?offset=%d&size=%d", frro_path_.c_str(),
191 static_cast<int> (FRRO_HEADER_SIZE + total_binary_bytes),
192 static_cast<int> (s.st_size));
193 total_binary_bytes += s.st_size;
194 binary_files.emplace_back(value.second.data_binary_value->get());
195 auto ref = string_pool.MakeRef(std::move(uri));
196 pb_value->set_data_value(ref.index());
197 } else {
198 pb_value->set_data_value(value.second.data_value);
199 }
200 }
201 }
202 }
203 }
204 android::BigBuffer string_buffer(kBufferSize);
205 android::StringPool::FlattenUtf8(&string_buffer, string_pool, nullptr);
206 return FabricatedOverlay(std::move(overlay_pb), string_buffer.to_string(),
207 std::move(binary_files), total_binary_bytes);
208 }
209
FromBinaryStream(std::istream & stream)210 Result<FabricatedOverlay> FabricatedOverlay::FromBinaryStream(std::istream& stream) {
211 uint32_t magic;
212 if (!Read32(stream, &magic)) {
213 return Error("Failed to read fabricated overlay magic.");
214 }
215
216 if (magic != kFabricatedOverlayMagic) {
217 return Error("Not a fabricated overlay file.");
218 }
219
220 uint32_t version;
221 if (!Read32(stream, &version)) {
222 return Error("Failed to read fabricated overlay version.");
223 }
224
225 if (version < 1 || version > 3) {
226 return Error("Invalid fabricated overlay version '%u'.", version);
227 }
228
229 uint32_t crc;
230 if (!Read32(stream, &crc)) {
231 return Error("Failed to read fabricated overlay crc.");
232 }
233
234 pb::FabricatedOverlay overlay{};
235 std::string sp_data;
236 uint32_t total_binary_bytes;
237 if (version == 3) {
238 if (!Read32(stream, &total_binary_bytes)) {
239 return Error("Failed read total binary bytes.");
240 }
241 stream.seekg(total_binary_bytes, std::istream::cur);
242 }
243 if (version >= 2) {
244 uint32_t sp_size;
245 if (!Read32(stream, &sp_size)) {
246 return Error("Failed read string pool size.");
247 }
248 std::string buf(sp_size, '\0');
249 if (!stream.read(buf.data(), sp_size)) {
250 return Error("Failed to read string pool.");
251 }
252 sp_data = buf;
253 }
254 if (!overlay.ParseFromIstream(&stream)) {
255 return Error("Failed read fabricated overlay proto.");
256 }
257
258 // If the proto version is the latest version, then the contents of the proto must be the same
259 // when the proto is re-serialized; otherwise, the crc must be calculated because migrating the
260 // proto to the latest version will likely change the contents of the fabricated overlay.
261 return FabricatedOverlay(std::move(overlay), std::move(sp_data), {}, total_binary_bytes,
262 version == kFabricatedOverlayCurrentVersion
263 ? std::optional<uint32_t>(crc)
264 : std::nullopt);
265 }
266
InitializeData() const267 Result<FabricatedOverlay::SerializedData*> FabricatedOverlay::InitializeData() const {
268 if (!data_.has_value()) {
269 auto pb_size = overlay_pb_.ByteSizeLong();
270 auto pb_data = std::unique_ptr<uint8_t[]>(new uint8_t[pb_size]);
271
272 // Ensure serialization is deterministic
273 google::protobuf::io::ArrayOutputStream array_stream(pb_data.get(), pb_size);
274 google::protobuf::io::CodedOutputStream output_stream(&array_stream);
275 output_stream.SetSerializationDeterministic(true);
276 overlay_pb_.SerializeWithCachedSizes(&output_stream);
277 if (output_stream.HadError() || pb_size != output_stream.ByteCount()) {
278 return Error("Failed to serialize fabricated overlay.");
279 }
280
281 // Calculate the crc using the proto data and the version.
282 uint32_t pb_crc = crc32(0L, Z_NULL, 0);
283 pb_crc = crc32(pb_crc, reinterpret_cast<const uint8_t*>(&kFabricatedOverlayCurrentVersion),
284 sizeof(uint32_t));
285 pb_crc = crc32(pb_crc, pb_data.get(), pb_size);
286
287 data_ = SerializedData{std::move(pb_data), pb_size, pb_crc, string_pool_data_};
288 }
289 return &(*data_);
290 }
GetCrc() const291 Result<uint32_t> FabricatedOverlay::GetCrc() const {
292 if (crc_from_disk_.has_value()) {
293 return *crc_from_disk_;
294 }
295 auto data = InitializeData();
296 if (!data) {
297 return data.GetError();
298 }
299 return (*data)->pb_crc;
300 }
301
ToBinaryStream(std::ostream & stream) const302 Result<Unit> FabricatedOverlay::ToBinaryStream(std::ostream& stream) const {
303 auto data = InitializeData();
304 if (!data) {
305 return data.GetError();
306 }
307
308 Write32(stream, kFabricatedOverlayMagic);
309 Write32(stream, kFabricatedOverlayCurrentVersion);
310 Write32(stream, (*data)->pb_crc);
311 Write32(stream, total_binary_bytes_);
312 std::string file_contents;
313 for (const android::base::borrowed_fd fd : binary_files_) {
314 if (!ReadFdToString(fd, &file_contents)) {
315 return Error("Failed to read binary file data.");
316 }
317 stream.write(file_contents.data(), file_contents.length());
318 }
319 Write32(stream, (*data)->sp_data.length());
320 stream.write((*data)->sp_data.data(), (*data)->sp_data.length());
321 if (stream.bad()) {
322 return Error("Failed to write string pool data.");
323 }
324 stream.write(reinterpret_cast<const char*>((*data)->pb_data.get()), (*data)->pb_data_size);
325 if (stream.bad()) {
326 return Error("Failed to write serialized fabricated overlay.");
327 }
328
329 return Unit{};
330 }
331
332 using FabContainer = FabricatedOverlayContainer;
FabricatedOverlayContainer(FabricatedOverlay && overlay,std::string && path)333 FabContainer::FabricatedOverlayContainer(FabricatedOverlay&& overlay, std::string&& path)
334 : overlay_(std::forward<FabricatedOverlay>(overlay)), path_(std::forward<std::string>(path)) {
335 }
336
337 FabContainer::~FabricatedOverlayContainer() = default;
338
FromPath(std::string path)339 Result<std::unique_ptr<FabContainer>> FabContainer::FromPath(std::string path) {
340 std::fstream fin(path);
341 auto overlay = FabricatedOverlay::FromBinaryStream(fin);
342 if (!overlay) {
343 return overlay.GetError();
344 }
345 return std::unique_ptr<FabContainer>(
346 new FabricatedOverlayContainer(std::move(*overlay), std::move(path)));
347 }
348
FromOverlay(FabricatedOverlay && overlay)349 std::unique_ptr<FabricatedOverlayContainer> FabContainer::FromOverlay(FabricatedOverlay&& overlay) {
350 return std::unique_ptr<FabContainer>(
351 new FabricatedOverlayContainer(std::move(overlay), {} /* path */));
352 }
353
GetManifestInfo() const354 OverlayManifestInfo FabContainer::GetManifestInfo() const {
355 const pb::FabricatedOverlay& overlay_pb = overlay_.overlay_pb_;
356 return OverlayManifestInfo{
357 .package_name = overlay_pb.package_name(),
358 .name = overlay_pb.name(),
359 .target_package = overlay_pb.target_package_name(),
360 .target_name = overlay_pb.target_overlayable(),
361 };
362 }
363
FindOverlayInfo(const std::string & name) const364 Result<OverlayManifestInfo> FabContainer::FindOverlayInfo(const std::string& name) const {
365 const OverlayManifestInfo info = GetManifestInfo();
366 if (name != info.name) {
367 return Error("Failed to find name '%s' in fabricated overlay", name.c_str());
368 }
369 return info;
370 }
371
GetOverlayData(const OverlayManifestInfo & info) const372 Result<OverlayData> FabContainer::GetOverlayData(const OverlayManifestInfo& info) const {
373 const pb::FabricatedOverlay& overlay_pb = overlay_.overlay_pb_;
374 if (info.name != overlay_pb.name()) {
375 return Error("Failed to find name '%s' in fabricated overlay", info.name.c_str());
376 }
377
378 OverlayData result{};
379 for (const auto& package : overlay_pb.packages()) {
380 for (const auto& type : package.types()) {
381 for (const auto& entry : type.entries()) {
382 auto name = base::StringPrintf("%s:%s/%s", package.name().c_str(), type.name().c_str(),
383 entry.name().c_str());
384 const auto& res_value = entry.res_value();
385 result.pairs.emplace_back(OverlayData::Value{
386 name, TargetValueWithConfig{.config = entry.configuration(), .value = TargetValue{
387 .data_type = static_cast<uint8_t>(res_value.data_type()),
388 .data_value = res_value.data_value()}}});
389 }
390 }
391 }
392 const uint32_t string_pool_data_length = overlay_.string_pool_data_.length();
393 result.string_pool_data = OverlayData::InlineStringPoolData{
394 .data = std::unique_ptr<uint8_t[]>(new uint8_t[string_pool_data_length]),
395 .data_length = string_pool_data_length,
396 .string_pool_offset = 0,
397 };
398 memcpy(result.string_pool_data->data.get(), overlay_.string_pool_data_.data(),
399 string_pool_data_length);
400 return result;
401 }
402
GetCrc() const403 Result<uint32_t> FabContainer::GetCrc() const {
404 return overlay_.GetCrc();
405 }
406
GetPath() const407 const std::string& FabContainer::GetPath() const {
408 return path_;
409 }
410
GetResourceName(ResourceId) const411 Result<std::string> FabContainer::GetResourceName(ResourceId /* id */) const {
412 return Error("Fabricated overlay does not contain resources.");
413 }
414
415 } // namespace android::idmap2
416