blob: 20aa7d32a3c2991dc0b6bfff6341b52cc8855aee [file] [log] [blame]
felkachang1039d612022-06-21 15:09:47 +08001/*
2 * Copyright (C) 2022 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 <sys/stat.h>
18
19#include <fstream>
20#include <optional>
21
22#define LOG_TAG "SelfTargeting"
23
24#include "androidfw/ResourceTypes.h"
25#include "idmap2/BinaryStreamVisitor.h"
26#include "idmap2/FabricatedOverlay.h"
27#include "idmap2/Idmap.h"
28#include "idmap2/Result.h"
29
30using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
31using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
32using android::idmap2::BinaryStreamVisitor;
33using android::idmap2::Idmap;
34using android::idmap2::OverlayResourceContainer;
35
36namespace android::self_targeting {
37
38constexpr const mode_t kIdmapFilePermission = S_IRUSR | S_IWUSR; // u=rw-, g=---, o=---
39
40extern "C" bool
41CreateFrroFile(std::string& out_err_result, std::string& packageName, std::string& overlayName,
42 std::string& targetPackageName, std::optional<std::string>& targetOverlayable,
43 std::vector<FabricatedOverlayEntryParameters>& entries_params,
44 const std::string& frro_file_path) {
45 android::idmap2::FabricatedOverlay::Builder builder(packageName, overlayName,
46 targetPackageName);
47 if (targetOverlayable.has_value()) {
48 builder.SetOverlayable(targetOverlayable.value_or(std::string()));
49 }
50 for (const auto& entry_params : entries_params) {
51 const auto dataType = entry_params.data_type;
52 if (entry_params.data_binary_value.has_value()) {
53 builder.SetResourceValue(entry_params.resource_name, *entry_params.data_binary_value,
54 entry_params.configuration);
55 } else if (dataType >= Res_value::TYPE_FIRST_INT && dataType <= Res_value::TYPE_LAST_INT) {
56 builder.SetResourceValue(entry_params.resource_name, dataType,
57 entry_params.data_value, entry_params.configuration);
58 } else if (dataType == Res_value::TYPE_STRING) {
59 builder.SetResourceValue(entry_params.resource_name, dataType,
60 entry_params.data_string_value , entry_params.configuration);
61 } else {
62 out_err_result = base::StringPrintf("Unsupported data type %d", dataType);
63 return false;
64 }
65 }
66
67 const auto frro = builder.Build();
68 std::ofstream fout(frro_file_path);
69 if (fout.fail()) {
70 out_err_result = base::StringPrintf("open output stream fail %s", std::strerror(errno));
71 return false;
72 }
73 auto result = frro->ToBinaryStream(fout);
74 if (!result) {
75 unlink(frro_file_path.c_str());
76 out_err_result = base::StringPrintf("to stream fail %s", result.GetErrorMessage().c_str());
77 return false;
78 }
79 fout.close();
80 if (fout.fail()) {
81 unlink(frro_file_path.c_str());
82 out_err_result = base::StringPrintf("output stream fail %s", std::strerror(errno));
83 return false;
84 }
85 if (chmod(frro_file_path.c_str(), kIdmapFilePermission) == -1) {
86 out_err_result = base::StringPrintf("Failed to change the file permission %s",
87 frro_file_path.c_str());
88 return false;
89 }
90 return true;
91}
92
93extern "C" bool
94CreateIdmapFile(std::string& out_err, const std::string& targetPath, const std::string& overlayPath,
95 const std::string& idmapPath, const std::string& overlayName) {
96 // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap
97 // guarantees that existing memory maps will continue to be valid and unaffected. The file must
98 // be deleted before attempting to create the idmap, so that if idmap creation fails, the
99 // overlay will no longer be usable.
100 unlink(idmapPath.c_str());
101
102 const auto target = idmap2::TargetResourceContainer::FromPath(targetPath);
103 if (!target) {
104 out_err = base::StringPrintf("Failed to load target %s because of %s", targetPath.c_str(),
105 target.GetErrorMessage().c_str());
106 return false;
107 }
108
109 const auto overlay = OverlayResourceContainer::FromPath(overlayPath);
110 if (!overlay) {
111 out_err = base::StringPrintf("Failed to load overlay %s because of %s", overlayPath.c_str(),
112 overlay.GetErrorMessage().c_str());
113 return false;
114 }
115
116 // Overlay self target process. Only allow self-targeting types.
117 const auto fulfilled_policies = static_cast<PolicyBitmask>(
118 PolicyFlags::PUBLIC | PolicyFlags::SYSTEM_PARTITION | PolicyFlags::VENDOR_PARTITION |
119 PolicyFlags::PRODUCT_PARTITION | PolicyFlags::SIGNATURE | PolicyFlags::ODM_PARTITION |
120 PolicyFlags::OEM_PARTITION | PolicyFlags::ACTOR_SIGNATURE |
121 PolicyFlags::CONFIG_SIGNATURE);
122
123 const auto idmap = Idmap::FromContainers(**target, **overlay, overlayName,
124 fulfilled_policies, false /* enforce_overlayable */);
125 if (!idmap) {
126 out_err = base::StringPrintf("Failed to create idmap because of %s",
127 idmap.GetErrorMessage().c_str());
128 return false;
129 }
130
131 std::ofstream fout(idmapPath.c_str());
132 if (fout.fail()) {
133 out_err = base::StringPrintf("Failed to create idmap %s because of %s", idmapPath.c_str(),
134 strerror(errno));
135 return false;
136 }
137
138 BinaryStreamVisitor visitor(fout);
139 (*idmap)->accept(&visitor);
140 fout.close();
141 if (fout.fail()) {
142 unlink(idmapPath.c_str());
143 out_err = base::StringPrintf("Failed to write idmap %s because of %s", idmapPath.c_str(),
144 strerror(errno));
145 return false;
146 }
147 if (chmod(idmapPath.c_str(), kIdmapFilePermission) == -1) {
148 out_err = base::StringPrintf("Failed to change the file permission %s", idmapPath.c_str());
149 return false;
150 }
151 return true;
152}
153
154extern "C" bool
155GetFabricatedOverlayInfo(std::string& out_err, const std::string& overlay_path,
156 OverlayManifestInfo& out_info) {
157 const auto overlay = idmap2::FabricatedOverlayContainer::FromPath(overlay_path);
158 if (!overlay) {
159 out_err = base::StringPrintf("Failed to write idmap %s because of %s",
160 overlay_path.c_str(), strerror(errno));
161 return false;
162 }
163
164 out_info = (*overlay)->GetManifestInfo();
165
166 return true;
167}
168
169} // namespace android::self_targeting
170