blob: 073d987f5dad80266eab870675758e040771014c [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2d/Idmap2Service.h"
18
Mårten Kongstad02751232018-04-27 13:16:32 +020019#include <sys/stat.h> // umask
20#include <sys/types.h> // umask
Mårten Kongstad02751232018-04-27 13:16:32 +020021
22#include <cerrno>
23#include <cstring>
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080024#include <filesystem>
Mårten Kongstad02751232018-04-27 13:16:32 +020025#include <fstream>
26#include <memory>
27#include <ostream>
28#include <string>
Mårten Kongstad1195a6b2021-05-11 12:57:01 +000029#include <utility>
30#include <vector>
Mårten Kongstad02751232018-04-27 13:16:32 +020031
32#include "android-base/macros.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080033#include "android-base/stringprintf.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010034#include "binder/IPCThreadState.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020035#include "idmap2/BinaryStreamVisitor.h"
36#include "idmap2/FileUtils.h"
37#include "idmap2/Idmap.h"
Mårten Kongstad99ae8982021-05-10 11:00:17 +000038#include "idmap2/PrettyPrintVisitor.h"
Ryan Mitchella7070132020-05-13 14:17:52 -070039#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010040#include "idmap2/SysTrace.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020041
Ryan Mitchella7070132020-05-13 14:17:52 -070042using android::base::StringPrintf;
Mårten Kongstad02751232018-04-27 13:16:32 +020043using android::binder::Status;
44using android::idmap2::BinaryStreamVisitor;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080045using android::idmap2::FabricatedOverlayContainer;
Mårten Kongstad02751232018-04-27 13:16:32 +020046using android::idmap2::Idmap;
47using android::idmap2::IdmapHeader;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080048using android::idmap2::OverlayResourceContainer;
Mårten Kongstad99ae8982021-05-10 11:00:17 +000049using android::idmap2::PrettyPrintVisitor;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080050using android::idmap2::TargetResourceContainer;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010051using android::idmap2::utils::kIdmapCacheDir;
Mårten Kongstadb8779022018-11-29 09:53:17 +010052using android::idmap2::utils::kIdmapFilePermissionMask;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080053using android::idmap2::utils::RandomStringForPath;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010054using android::idmap2::utils::UidHasWriteAccessToPath;
Mårten Kongstad02751232018-04-27 13:16:32 +020055
Winson62ac8b52019-12-04 08:36:48 -080056using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
57
Mårten Kongstad02751232018-04-27 13:16:32 +020058namespace {
59
Ryan Mitchell7d53f192020-04-24 17:45:25 -070060constexpr const char* kFrameworkPath = "/system/framework/framework-res.apk";
61
Mårten Kongstad02751232018-04-27 13:16:32 +020062Status ok() {
63 return Status::ok();
64}
65
66Status error(const std::string& msg) {
67 LOG(ERROR) << msg;
68 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
69}
70
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080071PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
72 return static_cast<PolicyBitmask>(arg);
73}
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080074
Mårten Kongstad02751232018-04-27 13:16:32 +020075} // namespace
76
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010077namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020078
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080079Status Idmap2Service::getIdmapPath(const std::string& overlay_path,
Mårten Kongstad02751232018-04-27 13:16:32 +020080 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
81 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080082 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_path;
83 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +020084 return ok();
85}
86
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080087Status Idmap2Service::removeIdmap(const std::string& overlay_path, int32_t user_id ATTRIBUTE_UNUSED,
88 bool* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +020089 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080090 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010091 const uid_t uid = IPCThreadState::self()->getCallingUid();
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080092 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010093 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
94 *_aidl_return = false;
95 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
96 idmap_path.c_str(), uid));
97 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010098 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020099 *_aidl_return = false;
100 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
101 }
Mårten Kongstadb8779022018-11-29 09:53:17 +0100102 *_aidl_return = true;
103 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +0200104}
105
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800106Status Idmap2Service::verifyIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800107 const std::string& overlay_name, int32_t fulfilled_policies,
108 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
109 bool* _aidl_return) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800110 SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_path;
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100111 assert(_aidl_return);
Ryan Mitchell038a2842020-06-08 14:41:07 -0700112
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800113 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100114 std::ifstream fin(idmap_path);
115 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
116 fin.close();
hg.choia3a68132020-01-15 17:12:48 +0900117 if (!header) {
118 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800119 LOG(WARNING) << "failed to parse idmap header of '" << idmap_path << "'";
120 return ok();
hg.choia3a68132020-01-15 17:12:48 +0900121 }
122
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800123 const auto target = GetTargetContainer(target_path);
124 if (!target) {
Ryan Mitchella7070132020-05-13 14:17:52 -0700125 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800126 LOG(WARNING) << "failed to load target '" << target_path << "'";
127 return ok();
128 }
129
130 const auto overlay = OverlayResourceContainer::FromPath(overlay_path);
131 if (!overlay) {
132 *_aidl_return = false;
133 LOG(WARNING) << "failed to load overlay '" << overlay_path << "'";
134 return ok();
Ryan Mitchella7070132020-05-13 14:17:52 -0700135 }
136
137 auto up_to_date =
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800138 header->IsUpToDate(*GetPointer(*target), **overlay, overlay_name,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700139 ConvertAidlArgToPolicyBitmask(fulfilled_policies), enforce_overlayable);
Ryan Mitchella7070132020-05-13 14:17:52 -0700140
Ryan Mitchell038a2842020-06-08 14:41:07 -0700141 *_aidl_return = static_cast<bool>(up_to_date);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800142 if (!up_to_date) {
143 LOG(WARNING) << "idmap '" << idmap_path
144 << "' not up to date : " << up_to_date.GetErrorMessage();
145 }
146 return ok();
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100147}
148
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800149Status Idmap2Service::createIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800150 const std::string& overlay_name, int32_t fulfilled_policies,
151 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
Jooyung Han16bac852020-08-10 12:53:14 +0900152 std::optional<std::string>* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200153 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800154 SYSTRACE << "Idmap2Service::createIdmap " << target_path << " " << overlay_path;
Jooyung Han605e39f2020-01-23 13:29:22 +0900155 _aidl_return->reset();
Mårten Kongstad02751232018-04-27 13:16:32 +0200156
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800157 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
158
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800159 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100160 const uid_t uid = IPCThreadState::self()->getCallingUid();
161 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
162 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss",
163 idmap_path.c_str(), uid));
164 }
165
Ryan Mitchellb49cb5e2021-02-10 11:27:29 -0800166 // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap guarantees
167 // that existing memory maps will continue to be valid and unaffected. The file must be deleted
168 // before attempting to create the idmap, so that if idmap creation fails, the overlay will no
169 // longer be usable.
170 unlink(idmap_path.c_str());
171
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800172 const auto target = GetTargetContainer(target_path);
173 if (!target) {
174 return error("failed to load target '%s'" + target_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200175 }
176
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800177 const auto overlay = OverlayResourceContainer::FromPath(overlay_path);
178 if (!overlay) {
179 return error("failed to load apk overlay '%s'" + overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200180 }
181
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800182 const auto idmap = Idmap::FromContainers(*GetPointer(*target), **overlay, overlay_name,
183 policy_bitmask, enforce_overlayable);
Mårten Kongstad02751232018-04-27 13:16:32 +0200184 if (!idmap) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100185 return error(idmap.GetErrorMessage());
Mårten Kongstad02751232018-04-27 13:16:32 +0200186 }
187
Mårten Kongstadb8779022018-11-29 09:53:17 +0100188 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200189 std::ofstream fout(idmap_path);
190 if (fout.fail()) {
191 return error("failed to open idmap path " + idmap_path);
192 }
Ryan Mitchella9093052020-03-26 17:15:01 -0700193
Mårten Kongstad02751232018-04-27 13:16:32 +0200194 BinaryStreamVisitor visitor(fout);
Mårten Kongstadce424902019-03-01 08:35:37 +0100195 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +0200196 fout.close();
197 if (fout.fail()) {
Ryan Mitchella9093052020-03-26 17:15:01 -0700198 unlink(idmap_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200199 return error("failed to write to idmap path " + idmap_path);
200 }
201
Jooyung Han16bac852020-08-10 12:53:14 +0900202 *_aidl_return = idmap_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200203 return ok();
204}
205
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800206idmap2::Result<Idmap2Service::TargetResourceContainerPtr> Idmap2Service::GetTargetContainer(
207 const std::string& target_path) {
208 if (target_path == kFrameworkPath) {
209 if (framework_apk_cache_ == nullptr) {
210 // Initialize the framework APK cache.
211 auto target = TargetResourceContainer::FromPath(target_path);
212 if (!target) {
213 return target.GetError();
214 }
215 framework_apk_cache_ = std::move(*target);
216 }
217 return {framework_apk_cache_.get()};
218 }
219
220 auto target = TargetResourceContainer::FromPath(target_path);
221 if (!target) {
222 return target.GetError();
223 }
224 return {std::move(*target)};
225}
226
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800227Status Idmap2Service::createFabricatedOverlay(
228 const os::FabricatedOverlayInternal& overlay,
229 std::optional<os::FabricatedOverlayInfo>* _aidl_return) {
230 idmap2::FabricatedOverlay::Builder builder(overlay.packageName, overlay.overlayName,
231 overlay.targetPackageName);
232 if (!overlay.targetOverlayable.empty()) {
233 builder.SetOverlayable(overlay.targetOverlayable);
234 }
235
236 for (const auto& res : overlay.entries) {
Jeremy Meyer05466592022-06-02 21:31:15 +0000237 if (res.dataType == Res_value::TYPE_STRING) {
238 builder.SetResourceValue(res.resourceName, res.dataType, res.stringData.value());
239 } else {
240 builder.SetResourceValue(res.resourceName, res.dataType, res.data);
241 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800242 }
243
244 // Generate the file path of the fabricated overlay and ensure it does not collide with an
245 // existing path. Re-registering a fabricated overlay will always result in an updated path.
246 std::string path;
247 std::string file_name;
248 do {
249 constexpr size_t kSuffixLength = 4;
250 const std::string random_suffix = RandomStringForPath(kSuffixLength);
251 file_name = StringPrintf("%s-%s-%s.frro", overlay.packageName.c_str(),
252 overlay.overlayName.c_str(), random_suffix.c_str());
253 path = StringPrintf("%s/%s", kIdmapCacheDir, file_name.c_str());
254
255 // Invoking std::filesystem::exists with a file name greater than 255 characters will cause this
256 // process to abort since the name exceeds the maximum file name size.
257 const size_t kMaxFileNameLength = 255;
258 if (file_name.size() > kMaxFileNameLength) {
259 return error(
260 base::StringPrintf("fabricated overlay file name '%s' longer than %zu characters",
261 file_name.c_str(), kMaxFileNameLength));
262 }
263 } while (std::filesystem::exists(path));
264
265 const uid_t uid = IPCThreadState::self()->getCallingUid();
266 if (!UidHasWriteAccessToPath(uid, path)) {
267 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write access",
268 path.c_str(), uid));
269 }
270
Mårten Kongstada384fb72021-05-10 08:34:54 +0000271 const auto frro = builder.Build();
272 if (!frro) {
273 return error(StringPrintf("failed to serialize '%s:%s': %s", overlay.packageName.c_str(),
274 overlay.overlayName.c_str(), frro.GetErrorMessage().c_str()));
275 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800276 // Persist the fabricated overlay.
277 umask(kIdmapFilePermissionMask);
278 std::ofstream fout(path);
279 if (fout.fail()) {
280 return error("failed to open frro path " + path);
281 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800282 auto result = frro->ToBinaryStream(fout);
283 if (!result) {
284 unlink(path.c_str());
285 return error("failed to write to frro path " + path + ": " + result.GetErrorMessage());
286 }
287 if (fout.fail()) {
288 unlink(path.c_str());
289 return error("failed to write to frro path " + path);
290 }
291
292 os::FabricatedOverlayInfo out_info;
293 out_info.packageName = overlay.packageName;
294 out_info.overlayName = overlay.overlayName;
295 out_info.targetPackageName = overlay.targetPackageName;
296 out_info.targetOverlayable = overlay.targetOverlayable;
297 out_info.path = path;
298 *_aidl_return = out_info;
299 return ok();
300}
301
Ryan Mitchellb887f682021-07-15 10:43:42 -0700302Status Idmap2Service::acquireFabricatedOverlayIterator() {
303 if (frro_iter_.has_value()) {
304 LOG(WARNING) << "active ffro iterator was not previously released";
305 }
306 frro_iter_ = std::filesystem::directory_iterator(kIdmapCacheDir);
307 return ok();
308}
309
310Status Idmap2Service::releaseFabricatedOverlayIterator() {
311 if (!frro_iter_.has_value()) {
312 LOG(WARNING) << "no active ffro iterator to release";
313 }
314 return ok();
315}
316
317Status Idmap2Service::nextFabricatedOverlayInfos(
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800318 std::vector<os::FabricatedOverlayInfo>* _aidl_return) {
Ryan Mitchellb887f682021-07-15 10:43:42 -0700319 constexpr size_t kMaxEntryCount = 100;
320 if (!frro_iter_.has_value()) {
321 return error("no active frro iterator");
322 }
323
324 size_t count = 0;
325 auto& entry_iter = *frro_iter_;
326 auto entry_iter_end = end(*frro_iter_);
327 for (; entry_iter != entry_iter_end && count < kMaxEntryCount; ++entry_iter) {
328 auto& entry = *entry_iter;
329 if (!entry.is_regular_file() || !android::IsFabricatedOverlay(entry.path())) {
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800330 continue;
331 }
332
333 const auto overlay = FabricatedOverlayContainer::FromPath(entry.path());
334 if (!overlay) {
Ryan Mitchellb887f682021-07-15 10:43:42 -0700335 LOG(WARNING) << "Failed to open '" << entry.path() << "': " << overlay.GetErrorMessage();
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800336 continue;
337 }
338
339 const auto info = (*overlay)->GetManifestInfo();
340 os::FabricatedOverlayInfo out_info;
341 out_info.packageName = info.package_name;
342 out_info.overlayName = info.name;
343 out_info.targetPackageName = info.target_package;
344 out_info.targetOverlayable = info.target_name;
345 out_info.path = entry.path();
346 _aidl_return->emplace_back(std::move(out_info));
Ryan Mitchellb887f682021-07-15 10:43:42 -0700347 count++;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800348 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800349 return ok();
350}
351
352binder::Status Idmap2Service::deleteFabricatedOverlay(const std::string& overlay_path,
353 bool* _aidl_return) {
354 SYSTRACE << "Idmap2Service::deleteFabricatedOverlay " << overlay_path;
355 const uid_t uid = IPCThreadState::self()->getCallingUid();
356
357 if (!UidHasWriteAccessToPath(uid, overlay_path)) {
358 *_aidl_return = false;
359 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
360 overlay_path.c_str(), uid));
361 }
362
363 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
364 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
365 *_aidl_return = false;
366 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
367 idmap_path.c_str(), uid));
368 }
369
370 if (unlink(overlay_path.c_str()) != 0) {
371 *_aidl_return = false;
372 return error("failed to unlink " + overlay_path + ": " + strerror(errno));
373 }
374
375 if (unlink(idmap_path.c_str()) != 0) {
376 *_aidl_return = false;
377 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
378 }
379
380 *_aidl_return = true;
381 return ok();
382}
383
Mårten Kongstad99ae8982021-05-10 11:00:17 +0000384binder::Status Idmap2Service::dumpIdmap(const std::string& overlay_path,
385 std::string* _aidl_return) {
386 assert(_aidl_return);
387
388 const auto idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
389 std::ifstream fin(idmap_path);
390 const auto idmap = Idmap::FromBinaryStream(fin);
391 fin.close();
392 if (!idmap) {
393 return error(idmap.GetErrorMessage());
394 }
395
396 std::stringstream stream;
397 PrettyPrintVisitor visitor(stream);
398 (*idmap)->accept(&visitor);
399 *_aidl_return = stream.str();
400
401 return ok();
402}
403
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100404} // namespace android::os