blob: f264125cfde542f2bd108c799a95c090bee67f2b [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>
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -070026#include <limits>
Mårten Kongstad02751232018-04-27 13:16:32 +020027#include <memory>
28#include <ostream>
29#include <string>
Mårten Kongstad1195a6b2021-05-11 12:57:01 +000030#include <utility>
31#include <vector>
Mårten Kongstad02751232018-04-27 13:16:32 +020032
33#include "android-base/macros.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080034#include "android-base/stringprintf.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010035#include "binder/IPCThreadState.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020036#include "idmap2/BinaryStreamVisitor.h"
37#include "idmap2/FileUtils.h"
38#include "idmap2/Idmap.h"
Mårten Kongstad99ae8982021-05-10 11:00:17 +000039#include "idmap2/PrettyPrintVisitor.h"
Ryan Mitchella7070132020-05-13 14:17:52 -070040#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010041#include "idmap2/SysTrace.h"
Jeremy Meyera761f332022-10-21 17:42:14 +000042#include <fcntl.h>
Mårten Kongstad02751232018-04-27 13:16:32 +020043
Ryan Mitchella7070132020-05-13 14:17:52 -070044using android::base::StringPrintf;
Mårten Kongstad02751232018-04-27 13:16:32 +020045using android::binder::Status;
46using android::idmap2::BinaryStreamVisitor;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080047using android::idmap2::FabricatedOverlayContainer;
Mårten Kongstad02751232018-04-27 13:16:32 +020048using android::idmap2::Idmap;
49using android::idmap2::IdmapHeader;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080050using android::idmap2::OverlayResourceContainer;
Mårten Kongstad99ae8982021-05-10 11:00:17 +000051using android::idmap2::PrettyPrintVisitor;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080052using android::idmap2::TargetResourceContainer;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010053using android::idmap2::utils::kIdmapCacheDir;
Mårten Kongstadb8779022018-11-29 09:53:17 +010054using android::idmap2::utils::kIdmapFilePermissionMask;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080055using android::idmap2::utils::RandomStringForPath;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010056using android::idmap2::utils::UidHasWriteAccessToPath;
Mårten Kongstad02751232018-04-27 13:16:32 +020057
Winson62ac8b52019-12-04 08:36:48 -080058using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
59
Mårten Kongstad02751232018-04-27 13:16:32 +020060namespace {
61
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -070062constexpr std::string_view kFrameworkPath = "/system/framework/framework-res.apk";
Ryan Mitchell7d53f192020-04-24 17:45:25 -070063
Mårten Kongstad02751232018-04-27 13:16:32 +020064Status ok() {
65 return Status::ok();
66}
67
68Status error(const std::string& msg) {
69 LOG(ERROR) << msg;
70 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
71}
72
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080073PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
74 return static_cast<PolicyBitmask>(arg);
75}
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080076
Mårten Kongstad02751232018-04-27 13:16:32 +020077} // namespace
78
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010079namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020080
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080081Status Idmap2Service::getIdmapPath(const std::string& overlay_path,
Mårten Kongstad02751232018-04-27 13:16:32 +020082 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
83 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080084 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_path;
85 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +020086 return ok();
87}
88
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080089Status Idmap2Service::removeIdmap(const std::string& overlay_path, int32_t user_id ATTRIBUTE_UNUSED,
90 bool* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +020091 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080092 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010093 const uid_t uid = IPCThreadState::self()->getCallingUid();
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080094 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010095 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
96 *_aidl_return = false;
97 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
98 idmap_path.c_str(), uid));
99 }
Mårten Kongstadb8779022018-11-29 09:53:17 +0100100 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200101 *_aidl_return = false;
102 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
103 }
Mårten Kongstadb8779022018-11-29 09:53:17 +0100104 *_aidl_return = true;
105 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +0200106}
107
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800108Status Idmap2Service::verifyIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800109 const std::string& overlay_name, int32_t fulfilled_policies,
110 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
111 bool* _aidl_return) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800112 SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_path;
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100113 assert(_aidl_return);
Ryan Mitchell038a2842020-06-08 14:41:07 -0700114
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800115 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100116 std::ifstream fin(idmap_path);
117 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
118 fin.close();
hg.choia3a68132020-01-15 17:12:48 +0900119 if (!header) {
120 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800121 LOG(WARNING) << "failed to parse idmap header of '" << idmap_path << "'";
122 return ok();
hg.choia3a68132020-01-15 17:12:48 +0900123 }
124
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800125 const auto target = GetTargetContainer(target_path);
126 if (!target) {
Ryan Mitchella7070132020-05-13 14:17:52 -0700127 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800128 LOG(WARNING) << "failed to load target '" << target_path << "'";
129 return ok();
130 }
131
132 const auto overlay = OverlayResourceContainer::FromPath(overlay_path);
133 if (!overlay) {
134 *_aidl_return = false;
135 LOG(WARNING) << "failed to load overlay '" << overlay_path << "'";
136 return ok();
Ryan Mitchella7070132020-05-13 14:17:52 -0700137 }
138
139 auto up_to_date =
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800140 header->IsUpToDate(*GetPointer(*target), **overlay, overlay_name,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700141 ConvertAidlArgToPolicyBitmask(fulfilled_policies), enforce_overlayable);
Ryan Mitchella7070132020-05-13 14:17:52 -0700142
Ryan Mitchell038a2842020-06-08 14:41:07 -0700143 *_aidl_return = static_cast<bool>(up_to_date);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800144 if (!up_to_date) {
145 LOG(WARNING) << "idmap '" << idmap_path
146 << "' not up to date : " << up_to_date.GetErrorMessage();
147 }
148 return ok();
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100149}
150
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800151Status Idmap2Service::createIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800152 const std::string& overlay_name, int32_t fulfilled_policies,
153 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
Jooyung Han16bac852020-08-10 12:53:14 +0900154 std::optional<std::string>* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200155 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800156 SYSTRACE << "Idmap2Service::createIdmap " << target_path << " " << overlay_path;
Jooyung Han605e39f2020-01-23 13:29:22 +0900157 _aidl_return->reset();
Mårten Kongstad02751232018-04-27 13:16:32 +0200158
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800159 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
160
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800161 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100162 const uid_t uid = IPCThreadState::self()->getCallingUid();
163 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
164 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss",
165 idmap_path.c_str(), uid));
166 }
167
Ryan Mitchellb49cb5e2021-02-10 11:27:29 -0800168 // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap guarantees
169 // that existing memory maps will continue to be valid and unaffected. The file must be deleted
170 // before attempting to create the idmap, so that if idmap creation fails, the overlay will no
171 // longer be usable.
172 unlink(idmap_path.c_str());
173
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800174 const auto target = GetTargetContainer(target_path);
175 if (!target) {
176 return error("failed to load target '%s'" + target_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200177 }
178
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800179 const auto overlay = OverlayResourceContainer::FromPath(overlay_path);
180 if (!overlay) {
181 return error("failed to load apk overlay '%s'" + overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200182 }
183
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800184 const auto idmap = Idmap::FromContainers(*GetPointer(*target), **overlay, overlay_name,
185 policy_bitmask, enforce_overlayable);
Mårten Kongstad02751232018-04-27 13:16:32 +0200186 if (!idmap) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100187 return error(idmap.GetErrorMessage());
Mårten Kongstad02751232018-04-27 13:16:32 +0200188 }
189
Mårten Kongstadb8779022018-11-29 09:53:17 +0100190 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200191 std::ofstream fout(idmap_path);
192 if (fout.fail()) {
193 return error("failed to open idmap path " + idmap_path);
194 }
Ryan Mitchella9093052020-03-26 17:15:01 -0700195
Mårten Kongstad02751232018-04-27 13:16:32 +0200196 BinaryStreamVisitor visitor(fout);
Mårten Kongstadce424902019-03-01 08:35:37 +0100197 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +0200198 fout.close();
199 if (fout.fail()) {
Ryan Mitchella9093052020-03-26 17:15:01 -0700200 unlink(idmap_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200201 return error("failed to write to idmap path " + idmap_path);
202 }
203
Jooyung Han16bac852020-08-10 12:53:14 +0900204 *_aidl_return = idmap_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200205 return ok();
206}
207
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800208idmap2::Result<Idmap2Service::TargetResourceContainerPtr> Idmap2Service::GetTargetContainer(
209 const std::string& target_path) {
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700210 const bool is_framework = target_path == kFrameworkPath;
211 bool use_cache;
212 struct stat st = {};
213 if (is_framework || !::stat(target_path.c_str(), &st)) {
214 use_cache = true;
215 } else {
216 LOG(WARNING) << "failed to stat target path '" << target_path << "' for the cache";
217 use_cache = false;
218 }
219
220 if (use_cache) {
221 std::lock_guard lock(container_cache_mutex_);
222 if (auto cache_it = container_cache_.find(target_path); cache_it != container_cache_.end()) {
223 const auto& item = cache_it->second;
224 if (is_framework ||
225 (item.dev == st.st_dev && item.inode == st.st_ino && item.size == st.st_size
226 && item.mtime.tv_sec == st.st_mtim.tv_sec && item.mtime.tv_nsec == st.st_mtim.tv_nsec)) {
227 return {item.apk.get()};
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800228 }
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700229 container_cache_.erase(cache_it);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800230 }
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800231 }
232
233 auto target = TargetResourceContainer::FromPath(target_path);
234 if (!target) {
235 return target.GetError();
236 }
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700237 if (!use_cache) {
238 return {std::move(*target)};
239 }
240
241 const auto res = target->get();
242 std::lock_guard lock(container_cache_mutex_);
243 container_cache_.emplace(target_path, CachedContainer {
244 .dev = dev_t(st.st_dev),
245 .inode = ino_t(st.st_ino),
246 .size = st.st_size,
247 .mtime = st.st_mtim,
248 .apk = std::move(*target)
249 });
250 return {res};
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800251}
252
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800253Status Idmap2Service::createFabricatedOverlay(
254 const os::FabricatedOverlayInternal& overlay,
255 std::optional<os::FabricatedOverlayInfo>* _aidl_return) {
256 idmap2::FabricatedOverlay::Builder builder(overlay.packageName, overlay.overlayName,
257 overlay.targetPackageName);
258 if (!overlay.targetOverlayable.empty()) {
259 builder.SetOverlayable(overlay.targetOverlayable);
260 }
261
262 for (const auto& res : overlay.entries) {
Jeremy Meyer05466592022-06-02 21:31:15 +0000263 if (res.dataType == Res_value::TYPE_STRING) {
Jeremy Meyer77c8a932022-08-18 22:06:55 +0000264 builder.SetResourceValue(res.resourceName, res.dataType, res.stringData.value(),
265 res.configuration.value_or(std::string()));
Jeremy Meyera761f332022-10-21 17:42:14 +0000266 } else if (res.binaryData.has_value()) {
267 builder.SetResourceValue(res.resourceName, res.binaryData->get(),
Jeremy Meyerc04bcb32023-07-27 22:49:14 +0000268 res.binaryDataOffset, res.binaryDataSize,
Jeremy Meyerdeb46f312023-11-08 12:20:24 -0800269 res.configuration.value_or(std::string()),
270 res.isNinePatch);
Jeremy Meyer05466592022-06-02 21:31:15 +0000271 } else {
Jeremy Meyer77c8a932022-08-18 22:06:55 +0000272 builder.SetResourceValue(res.resourceName, res.dataType, res.data,
273 res.configuration.value_or(std::string()));
Jeremy Meyer05466592022-06-02 21:31:15 +0000274 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800275 }
276
277 // Generate the file path of the fabricated overlay and ensure it does not collide with an
278 // existing path. Re-registering a fabricated overlay will always result in an updated path.
279 std::string path;
280 std::string file_name;
281 do {
282 constexpr size_t kSuffixLength = 4;
283 const std::string random_suffix = RandomStringForPath(kSuffixLength);
284 file_name = StringPrintf("%s-%s-%s.frro", overlay.packageName.c_str(),
285 overlay.overlayName.c_str(), random_suffix.c_str());
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -0700286 path = StringPrintf("%s/%s", kIdmapCacheDir.data(), file_name.c_str());
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800287
288 // Invoking std::filesystem::exists with a file name greater than 255 characters will cause this
289 // process to abort since the name exceeds the maximum file name size.
290 const size_t kMaxFileNameLength = 255;
291 if (file_name.size() > kMaxFileNameLength) {
292 return error(
293 base::StringPrintf("fabricated overlay file name '%s' longer than %zu characters",
294 file_name.c_str(), kMaxFileNameLength));
295 }
296 } while (std::filesystem::exists(path));
Jeremy Meyera761f332022-10-21 17:42:14 +0000297 builder.setFrroPath(path);
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800298
299 const uid_t uid = IPCThreadState::self()->getCallingUid();
300 if (!UidHasWriteAccessToPath(uid, path)) {
301 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write access",
302 path.c_str(), uid));
303 }
304
Mårten Kongstada384fb72021-05-10 08:34:54 +0000305 const auto frro = builder.Build();
306 if (!frro) {
307 return error(StringPrintf("failed to serialize '%s:%s': %s", overlay.packageName.c_str(),
308 overlay.overlayName.c_str(), frro.GetErrorMessage().c_str()));
309 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800310 // Persist the fabricated overlay.
311 umask(kIdmapFilePermissionMask);
312 std::ofstream fout(path);
313 if (fout.fail()) {
314 return error("failed to open frro path " + path);
315 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800316 auto result = frro->ToBinaryStream(fout);
317 if (!result) {
318 unlink(path.c_str());
319 return error("failed to write to frro path " + path + ": " + result.GetErrorMessage());
320 }
321 if (fout.fail()) {
322 unlink(path.c_str());
323 return error("failed to write to frro path " + path);
324 }
325
326 os::FabricatedOverlayInfo out_info;
327 out_info.packageName = overlay.packageName;
328 out_info.overlayName = overlay.overlayName;
329 out_info.targetPackageName = overlay.targetPackageName;
330 out_info.targetOverlayable = overlay.targetOverlayable;
331 out_info.path = path;
332 *_aidl_return = out_info;
333 return ok();
334}
335
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700336Status Idmap2Service::acquireFabricatedOverlayIterator(int32_t* _aidl_return) {
337 std::lock_guard l(frro_iter_mutex_);
Ryan Mitchellb887f682021-07-15 10:43:42 -0700338 if (frro_iter_.has_value()) {
339 LOG(WARNING) << "active ffro iterator was not previously released";
340 }
341 frro_iter_ = std::filesystem::directory_iterator(kIdmapCacheDir);
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700342 if (frro_iter_id_ == std::numeric_limits<int32_t>::max()) {
343 frro_iter_id_ = 0;
344 } else {
345 ++frro_iter_id_;
346 }
347 *_aidl_return = frro_iter_id_;
Ryan Mitchellb887f682021-07-15 10:43:42 -0700348 return ok();
349}
350
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700351Status Idmap2Service::releaseFabricatedOverlayIterator(int32_t iteratorId) {
352 std::lock_guard l(frro_iter_mutex_);
Ryan Mitchellb887f682021-07-15 10:43:42 -0700353 if (!frro_iter_.has_value()) {
354 LOG(WARNING) << "no active ffro iterator to release";
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700355 } else if (frro_iter_id_ != iteratorId) {
356 LOG(WARNING) << "incorrect iterator id in a call to release";
felkachange03cea42022-08-31 12:43:11 +0800357 } else {
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700358 frro_iter_.reset();
Ryan Mitchellb887f682021-07-15 10:43:42 -0700359 }
360 return ok();
361}
362
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700363Status Idmap2Service::nextFabricatedOverlayInfos(int32_t iteratorId,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800364 std::vector<os::FabricatedOverlayInfo>* _aidl_return) {
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700365 std::lock_guard l(frro_iter_mutex_);
366
Ryan Mitchellb887f682021-07-15 10:43:42 -0700367 constexpr size_t kMaxEntryCount = 100;
368 if (!frro_iter_.has_value()) {
369 return error("no active frro iterator");
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700370 } else if (frro_iter_id_ != iteratorId) {
371 return error("incorrect iterator id in a call to next");
Ryan Mitchellb887f682021-07-15 10:43:42 -0700372 }
373
374 size_t count = 0;
375 auto& entry_iter = *frro_iter_;
376 auto entry_iter_end = end(*frro_iter_);
377 for (; entry_iter != entry_iter_end && count < kMaxEntryCount; ++entry_iter) {
378 auto& entry = *entry_iter;
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700379 if (!entry.is_regular_file() || !android::IsFabricatedOverlay(entry.path().native())) {
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800380 continue;
381 }
382
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700383 const auto overlay = FabricatedOverlayContainer::FromPath(entry.path().native());
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800384 if (!overlay) {
Ryan Mitchellb887f682021-07-15 10:43:42 -0700385 LOG(WARNING) << "Failed to open '" << entry.path() << "': " << overlay.GetErrorMessage();
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800386 continue;
387 }
388
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700389 auto info = (*overlay)->GetManifestInfo();
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800390 os::FabricatedOverlayInfo out_info;
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700391 out_info.packageName = std::move(info.package_name);
392 out_info.overlayName = std::move(info.name);
393 out_info.targetPackageName = std::move(info.target_package);
394 out_info.targetOverlayable = std::move(info.target_name);
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800395 out_info.path = entry.path();
396 _aidl_return->emplace_back(std::move(out_info));
Ryan Mitchellb887f682021-07-15 10:43:42 -0700397 count++;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800398 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800399 return ok();
400}
401
402binder::Status Idmap2Service::deleteFabricatedOverlay(const std::string& overlay_path,
403 bool* _aidl_return) {
404 SYSTRACE << "Idmap2Service::deleteFabricatedOverlay " << overlay_path;
405 const uid_t uid = IPCThreadState::self()->getCallingUid();
406
407 if (!UidHasWriteAccessToPath(uid, overlay_path)) {
408 *_aidl_return = false;
409 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
410 overlay_path.c_str(), uid));
411 }
412
413 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
414 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
415 *_aidl_return = false;
416 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
417 idmap_path.c_str(), uid));
418 }
419
420 if (unlink(overlay_path.c_str()) != 0) {
421 *_aidl_return = false;
422 return error("failed to unlink " + overlay_path + ": " + strerror(errno));
423 }
424
425 if (unlink(idmap_path.c_str()) != 0) {
426 *_aidl_return = false;
427 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
428 }
429
430 *_aidl_return = true;
431 return ok();
432}
433
Mårten Kongstad99ae8982021-05-10 11:00:17 +0000434binder::Status Idmap2Service::dumpIdmap(const std::string& overlay_path,
435 std::string* _aidl_return) {
436 assert(_aidl_return);
437
438 const auto idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
439 std::ifstream fin(idmap_path);
440 const auto idmap = Idmap::FromBinaryStream(fin);
441 fin.close();
442 if (!idmap) {
443 return error(idmap.GetErrorMessage());
444 }
445
446 std::stringstream stream;
447 PrettyPrintVisitor visitor(stream);
448 (*idmap)->accept(&visitor);
449 *_aidl_return = stream.str();
450
451 return ok();
452}
453
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100454} // namespace android::os