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 <android-base/strings.h>
24 #include <androidfw/BigBuffer.h>
25 #include <androidfw/BigBufferStream.h>
26 #include <androidfw/FileStream.h>
27 #include <androidfw/Image.h>
28 #include <androidfw/Png.h>
29 #include <androidfw/ResourceUtils.h>
30 #include <androidfw/StringPiece.h>
31 #include <androidfw/StringPool.h>
32 #include <androidfw/Streams.h>
33 #include <google/protobuf/io/coded_stream.h>
34 #include <google/protobuf/io/zero_copy_stream_impl.h>
35 #include <utils/ByteOrder.h>
36 #include <zlib.h>
37
38 #include <fstream>
39 #include <map>
40 #include <memory>
41 #include <string>
42 #include <utility>
43 #include <sys/utsname.h>
44
45 namespace android::idmap2 {
46 constexpr auto kBufferSize = 1024;
47
48 namespace {
Read32(std::istream & stream,uint32_t * out)49 bool Read32(std::istream& stream, uint32_t* out) {
50 uint32_t value;
51 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
52 *out = dtohl(value);
53 return true;
54 }
55 return false;
56 }
57
Write32(std::ostream & stream,uint32_t value)58 void Write32(std::ostream& stream, uint32_t value) {
59 uint32_t x = htodl(value);
60 stream.write(reinterpret_cast<char*>(&x), sizeof(uint32_t));
61 }
62 } // namespace
63
FabricatedOverlay(pb::FabricatedOverlay && overlay,std::string && string_pool_data,std::vector<FabricatedOverlay::BinaryData> binary_files,off_t total_binary_bytes,std::optional<uint32_t> crc_from_disk)64 FabricatedOverlay::FabricatedOverlay(pb::FabricatedOverlay&& overlay,
65 std::string&& string_pool_data,
66 std::vector<FabricatedOverlay::BinaryData> binary_files,
67 off_t total_binary_bytes,
68 std::optional<uint32_t> crc_from_disk)
69 : overlay_pb_(std::forward<pb::FabricatedOverlay>(overlay)),
70 string_pool_data_(std::move(string_pool_data)),
71 binary_files_(std::move(binary_files)),
72 total_binary_bytes_(total_binary_bytes),
73 crc_from_disk_(crc_from_disk) {
74 }
75
Builder(const std::string & package_name,const std::string & name,const std::string & target_package_name)76 FabricatedOverlay::Builder::Builder(const std::string& package_name, const std::string& name,
77 const std::string& target_package_name) {
78 package_name_ = package_name;
79 name_ = name;
80 target_package_name_ = target_package_name;
81 }
82
SetOverlayable(const std::string & name)83 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetOverlayable(const std::string& name) {
84 target_overlayable_ = name;
85 return *this;
86 }
87
SetResourceValue(const std::string & resource_name,uint8_t data_type,uint32_t data_value,const std::string & configuration)88 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue(
89 const std::string& resource_name, uint8_t data_type, uint32_t data_value,
90 const std::string& configuration) {
91 entries_.emplace_back(
92 Entry{resource_name, data_type, data_value, "", std::nullopt, 0, 0, configuration, false});
93 return *this;
94 }
95
SetResourceValue(const std::string & resource_name,uint8_t data_type,const std::string & data_string_value,const std::string & configuration)96 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue(
97 const std::string& resource_name, uint8_t data_type, const std::string& data_string_value,
98 const std::string& configuration) {
99 entries_.emplace_back(
100 Entry{resource_name,
101 data_type,
102 0,
103 data_string_value,
104 std::nullopt,
105 0,
106 0,
107 configuration,
108 false});
109 return *this;
110 }
111
SetResourceValue(const std::string & resource_name,std::optional<android::base::borrowed_fd> && binary_value,off64_t data_binary_offset,size_t data_binary_size,const std::string & configuration,bool nine_patch)112 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue(
113 const std::string& resource_name, std::optional<android::base::borrowed_fd>&& binary_value,
114 off64_t data_binary_offset, size_t data_binary_size, const std::string& configuration,
115 bool nine_patch) {
116 entries_.emplace_back(Entry{resource_name, 0, 0, "", binary_value,
117 data_binary_offset, data_binary_size, configuration, nine_patch});
118 return *this;
119 }
120
buildBinaryData(pb::ResourceValue * pb_value,const TargetValue & value)121 static Result<FabricatedOverlay::BinaryData> buildBinaryData(
122 pb::ResourceValue* pb_value, const TargetValue &value) {
123 pb_value->set_data_type(Res_value::TYPE_STRING);
124 size_t binary_size;
125 off64_t binary_offset;
126 std::unique_ptr<android::InputStream> binary_stream;
127
128 if (value.nine_patch) {
129 std::string file_contents;
130 file_contents.resize(value.data_binary_size);
131 if (!base::ReadFullyAtOffset(value.data_binary_value->get(), file_contents.data(),
132 value.data_binary_size, value.data_binary_offset)) {
133 return Error("Failed to read binary file data.");
134 }
135 const StringPiece content(file_contents.c_str(), file_contents.size());
136 android::PngChunkFilter png_chunk_filter(content);
137 android::AndroidLogDiagnostics diag;
138 auto png = android::ReadPng(&png_chunk_filter, &diag);
139 if (!png) {
140 return Error("Error opening file as png");
141 }
142
143 std::string err;
144 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(png->rows.get(),
145 png->width, png->height,
146 &err);
147 if (!nine_patch) {
148 return Error("%s", err.c_str());
149 }
150
151 png->width -= 2;
152 png->height -= 2;
153 memmove(png->rows.get(), png->rows.get() + 1, png->height * sizeof(uint8_t**));
154 for (int32_t h = 0; h < png->height; h++) {
155 memmove(png->rows[h], png->rows[h] + 4, png->width * 4);
156 }
157
158 android::BigBuffer buffer(value.data_binary_size);
159 android::BigBufferOutputStream buffer_output_stream(&buffer);
160 if (!android::WritePng(png.get(), nine_patch.get(), &buffer_output_stream, {},
161 &diag, false)) {
162 return Error("Error writing frro png");
163 }
164
165 binary_size = buffer.size();
166 binary_offset = 0;
167 android::BigBufferInputStream *buffer_input_stream
168 = new android::BigBufferInputStream(std::move(buffer));
169 binary_stream.reset(buffer_input_stream);
170 } else {
171 binary_size = value.data_binary_size;
172 binary_offset = value.data_binary_offset;
173 android::FileInputStream *fis
174 = new android::FileInputStream(value.data_binary_value.value());
175 binary_stream.reset(fis);
176 }
177
178 return FabricatedOverlay::BinaryData{
179 std::move(binary_stream),
180 binary_offset,
181 binary_size};
182 }
183
Build()184 Result<FabricatedOverlay> FabricatedOverlay::Builder::Build() {
185 using ConfigMap = std::map<std::string, TargetValue, std::less<>>;
186 using EntryMap = std::map<std::string, ConfigMap, std::less<>>;
187 using TypeMap = std::map<std::string, EntryMap, std::less<>>;
188 using PackageMap = std::map<std::string, TypeMap, std::less<>>;
189 PackageMap package_map;
190 android::StringPool string_pool;
191 for (const auto& res_entry : entries_) {
192 StringPiece package_substr;
193 StringPiece type_name;
194 StringPiece entry_name;
195 if (!android::ExtractResourceName(StringPiece(res_entry.resource_name), &package_substr,
196 &type_name, &entry_name)) {
197 return Error("failed to parse resource name '%s'", res_entry.resource_name.c_str());
198 }
199
200 std::string_view package_name = package_substr.empty() ? target_package_name_ : package_substr;
201 if (type_name.empty()) {
202 return Error("resource name '%s' missing type name", res_entry.resource_name.c_str());
203 }
204
205 if (entry_name.empty()) {
206 return Error("resource name '%s' missing entry name", res_entry.resource_name.c_str());
207 }
208
209 auto package = package_map.find(package_name);
210 if (package == package_map.end()) {
211 package = package_map
212 .insert(std::make_pair(package_name, TypeMap()))
213 .first;
214 }
215
216 auto type = package->second.find(type_name);
217 if (type == package->second.end()) {
218 type = package->second.insert(std::make_pair(type_name, EntryMap())).first;
219 }
220
221 auto entry = type->second.find(entry_name);
222 if (entry == type->second.end()) {
223 entry = type->second.insert(std::make_pair(entry_name, ConfigMap())).first;
224 }
225
226 auto value = entry->second.find(res_entry.configuration);
227 if (value == entry->second.end()) {
228 value = entry->second.insert(std::make_pair(res_entry.configuration, TargetValue())).first;
229 }
230
231 value->second = TargetValue{res_entry.data_type, res_entry.data_value,
232 res_entry.data_string_value, res_entry.data_binary_value,
233 res_entry.data_binary_offset, res_entry.data_binary_size,
234 res_entry.nine_patch};
235 }
236
237 pb::FabricatedOverlay overlay_pb;
238 overlay_pb.set_package_name(package_name_);
239 overlay_pb.set_name(name_);
240 overlay_pb.set_target_package_name(target_package_name_);
241 overlay_pb.set_target_overlayable(target_overlayable_);
242
243 std::vector<FabricatedOverlay::BinaryData> binary_files;
244 size_t total_binary_bytes = 0;
245 // 16 for the number of bytes in the frro file before the binary data
246 const size_t FRRO_HEADER_SIZE = 16;
247
248 for (auto& package : package_map) {
249 auto package_pb = overlay_pb.add_packages();
250 package_pb->set_name(package.first);
251
252 for (auto& type : package.second) {
253 auto type_pb = package_pb->add_types();
254 type_pb->set_name(type.first);
255
256 for (auto& entry : type.second) {
257 for (const auto& value: entry.second) {
258 auto entry_pb = type_pb->add_entries();
259 entry_pb->set_name(entry.first);
260 entry_pb->set_configuration(value.first);
261 pb::ResourceValue* pb_value = entry_pb->mutable_res_value();
262 pb_value->set_data_type(value.second.data_type);
263 if (value.second.data_type == Res_value::TYPE_STRING) {
264 auto ref = string_pool.MakeRef(value.second.data_string_value);
265 pb_value->set_data_value(ref.index());
266 } else if (value.second.data_binary_value.has_value()) {
267 auto binary_data = buildBinaryData(pb_value, value.second);
268 if (!binary_data) {
269 return binary_data.GetError();
270 }
271 pb_value->set_data_type(Res_value::TYPE_STRING);
272
273 std::string uri
274 = StringPrintf("frro:/%s?offset=%d&size=%d", frro_path_.c_str(),
275 static_cast<int> (FRRO_HEADER_SIZE + total_binary_bytes),
276 static_cast<int> (binary_data->size));
277 total_binary_bytes += binary_data->size;
278 binary_files.emplace_back(std::move(*binary_data));
279 auto ref = string_pool.MakeRef(std::move(uri));
280 pb_value->set_data_value(ref.index());
281 } else {
282 pb_value->set_data_value(value.second.data_value);
283 }
284 }
285 }
286 }
287 }
288 android::BigBuffer string_buffer(kBufferSize);
289 android::StringPool::FlattenUtf8(&string_buffer, string_pool, nullptr);
290 return FabricatedOverlay(std::move(overlay_pb), string_buffer.to_string(),
291 std::move(binary_files), total_binary_bytes);
292 }
293
FromBinaryStream(std::istream & stream)294 Result<FabricatedOverlay> FabricatedOverlay::FromBinaryStream(std::istream& stream) {
295 uint32_t magic;
296 if (!Read32(stream, &magic)) {
297 return Error("Failed to read fabricated overlay magic.");
298 }
299
300 if (magic != kFabricatedOverlayMagic) {
301 return Error("Not a fabricated overlay file.");
302 }
303
304 uint32_t version;
305 if (!Read32(stream, &version)) {
306 return Error("Failed to read fabricated overlay version.");
307 }
308
309 if (version < 1 || version > 3) {
310 return Error("Invalid fabricated overlay version '%u'.", version);
311 }
312
313 uint32_t crc;
314 if (!Read32(stream, &crc)) {
315 return Error("Failed to read fabricated overlay crc.");
316 }
317
318 pb::FabricatedOverlay overlay{};
319 std::string sp_data;
320 uint32_t total_binary_bytes;
321 if (version == 3) {
322 if (!Read32(stream, &total_binary_bytes)) {
323 return Error("Failed read total binary bytes.");
324 }
325 stream.seekg(total_binary_bytes, std::istream::cur);
326 }
327 if (version >= 2) {
328 uint32_t sp_size;
329 if (!Read32(stream, &sp_size)) {
330 return Error("Failed read string pool size.");
331 }
332 std::string buf(sp_size, '\0');
333 if (!stream.read(buf.data(), sp_size)) {
334 return Error("Failed to read string pool.");
335 }
336 sp_data = buf;
337 }
338 if (!overlay.ParseFromIstream(&stream)) {
339 return Error("Failed read fabricated overlay proto.");
340 }
341
342 // If the proto version is the latest version, then the contents of the proto must be the same
343 // when the proto is re-serialized; otherwise, the crc must be calculated because migrating the
344 // proto to the latest version will likely change the contents of the fabricated overlay.
345 return FabricatedOverlay(std::move(overlay), std::move(sp_data), {}, total_binary_bytes,
346 version == kFabricatedOverlayCurrentVersion
347 ? std::optional<uint32_t>(crc)
348 : std::nullopt);
349 }
350
InitializeData() const351 Result<FabricatedOverlay::SerializedData*> FabricatedOverlay::InitializeData() const {
352 if (!data_.has_value()) {
353 auto pb_size = overlay_pb_.ByteSizeLong();
354 auto pb_data = std::unique_ptr<uint8_t[]>(new uint8_t[pb_size]);
355
356 // Ensure serialization is deterministic
357 google::protobuf::io::ArrayOutputStream array_stream(pb_data.get(), pb_size);
358 google::protobuf::io::CodedOutputStream output_stream(&array_stream);
359 output_stream.SetSerializationDeterministic(true);
360 overlay_pb_.SerializeWithCachedSizes(&output_stream);
361 if (output_stream.HadError() || pb_size != output_stream.ByteCount()) {
362 return Error("Failed to serialize fabricated overlay.");
363 }
364
365 // Calculate the crc using the proto data and the version.
366 uint32_t pb_crc = crc32(0L, Z_NULL, 0);
367 pb_crc = crc32(pb_crc, reinterpret_cast<const uint8_t*>(&kFabricatedOverlayCurrentVersion),
368 sizeof(uint32_t));
369 pb_crc = crc32(pb_crc, pb_data.get(), pb_size);
370
371 data_ = SerializedData{std::move(pb_data), pb_size, pb_crc, string_pool_data_};
372 }
373 return &(*data_);
374 }
GetCrc() const375 Result<uint32_t> FabricatedOverlay::GetCrc() const {
376 if (crc_from_disk_.has_value()) {
377 return *crc_from_disk_;
378 }
379 auto data = InitializeData();
380 if (!data) {
381 return data.GetError();
382 }
383 return (*data)->pb_crc;
384 }
385
ToBinaryStream(std::ostream & stream) const386 Result<Unit> FabricatedOverlay::ToBinaryStream(std::ostream& stream) const {
387 auto data = InitializeData();
388 if (!data) {
389 return data.GetError();
390 }
391
392 Write32(stream, kFabricatedOverlayMagic);
393 Write32(stream, kFabricatedOverlayCurrentVersion);
394 Write32(stream, (*data)->pb_crc);
395 Write32(stream, total_binary_bytes_);
396 std::string file_contents;
397 for (const FabricatedOverlay::BinaryData& bd : binary_files_) {
398 file_contents.resize(bd.size);
399 if (!bd.input_stream->ReadFullyAtOffset(file_contents.data(), bd.size, bd.offset)) {
400 return Error("Failed to read binary file data.");
401 }
402 stream.write(file_contents.data(), file_contents.length());
403 }
404 Write32(stream, (*data)->sp_data.length());
405 stream.write((*data)->sp_data.data(), (*data)->sp_data.length());
406 if (stream.bad()) {
407 return Error("Failed to write string pool data.");
408 }
409 stream.write(reinterpret_cast<const char*>((*data)->pb_data.get()), (*data)->pb_data_size);
410 if (stream.bad()) {
411 return Error("Failed to write serialized fabricated overlay.");
412 }
413
414 return Unit{};
415 }
416
417 using FabContainer = FabricatedOverlayContainer;
FabricatedOverlayContainer(FabricatedOverlay && overlay,std::string && path)418 FabContainer::FabricatedOverlayContainer(FabricatedOverlay&& overlay, std::string&& path)
419 : overlay_(std::forward<FabricatedOverlay>(overlay)), path_(std::forward<std::string>(path)) {
420 }
421
422 FabContainer::~FabricatedOverlayContainer() = default;
423
FromPath(std::string path)424 Result<std::unique_ptr<FabContainer>> FabContainer::FromPath(std::string path) {
425 std::fstream fin(path);
426 auto overlay = FabricatedOverlay::FromBinaryStream(fin);
427 if (!overlay) {
428 return overlay.GetError();
429 }
430 return std::unique_ptr<FabContainer>(
431 new FabricatedOverlayContainer(std::move(*overlay), std::move(path)));
432 }
433
FromOverlay(FabricatedOverlay && overlay)434 std::unique_ptr<FabricatedOverlayContainer> FabContainer::FromOverlay(FabricatedOverlay&& overlay) {
435 return std::unique_ptr<FabContainer>(
436 new FabricatedOverlayContainer(std::move(overlay), {} /* path */));
437 }
438
GetManifestInfo() const439 OverlayManifestInfo FabContainer::GetManifestInfo() const {
440 const pb::FabricatedOverlay& overlay_pb = overlay_.overlay_pb_;
441 return OverlayManifestInfo{
442 .package_name = overlay_pb.package_name(),
443 .name = overlay_pb.name(),
444 .target_package = overlay_pb.target_package_name(),
445 .target_name = overlay_pb.target_overlayable(),
446 };
447 }
448
FindOverlayInfo(const std::string & name) const449 Result<OverlayManifestInfo> FabContainer::FindOverlayInfo(const std::string& name) const {
450 const OverlayManifestInfo info = GetManifestInfo();
451 if (name != info.name) {
452 return Error("Failed to find name '%s' in fabricated overlay", name.c_str());
453 }
454 return info;
455 }
456
GetOverlayData(const OverlayManifestInfo & info) const457 Result<OverlayData> FabContainer::GetOverlayData(const OverlayManifestInfo& info) const {
458 const pb::FabricatedOverlay& overlay_pb = overlay_.overlay_pb_;
459 if (info.name != overlay_pb.name()) {
460 return Error("Failed to find name '%s' in fabricated overlay", info.name.c_str());
461 }
462
463 OverlayData result{};
464 for (const auto& package : overlay_pb.packages()) {
465 for (const auto& type : package.types()) {
466 for (const auto& entry : type.entries()) {
467 auto name = base::StringPrintf("%s:%s/%s", package.name().c_str(), type.name().c_str(),
468 entry.name().c_str());
469 const auto& res_value = entry.res_value();
470 result.pairs.emplace_back(OverlayData::Value{
471 name, TargetValueWithConfig{.value = TargetValue{
472 .data_type = static_cast<uint8_t>(res_value.data_type()),
473 .data_value = res_value.data_value()}, .config = entry.configuration()}});
474 }
475 }
476 }
477 const uint32_t string_pool_data_length = overlay_.string_pool_data_.length();
478 result.string_pool_data = OverlayData::InlineStringPoolData{
479 .data = std::unique_ptr<uint8_t[]>(new uint8_t[string_pool_data_length]),
480 .data_length = string_pool_data_length,
481 .string_pool_offset = 0,
482 };
483 memcpy(result.string_pool_data->data.get(), overlay_.string_pool_data_.data(),
484 string_pool_data_length);
485 return result;
486 }
487
GetCrc() const488 Result<uint32_t> FabContainer::GetCrc() const {
489 return overlay_.GetCrc();
490 }
491
GetPath() const492 const std::string& FabContainer::GetPath() const {
493 return path_;
494 }
495
GetResourceName(ResourceId) const496 Result<std::string> FabContainer::GetResourceName(ResourceId /* id */) const {
497 return Error("Fabricated overlay does not contain resources.");
498 }
499
500 } // namespace android::idmap2
501