blob: 7765d208284e13dc5679a8afa0f74e82ec8da007 [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";
Adnan Begovic14e307c12015-07-06 20:06:36 -070063constexpr std::string_view kOmniRomPath = "/system/framework/omnirom-res.apk";
Ryan Mitchell7d53f192020-04-24 17:45:25 -070064
Mårten Kongstad02751232018-04-27 13:16:32 +020065Status ok() {
66 return Status::ok();
67}
68
69Status error(const std::string& msg) {
70 LOG(ERROR) << msg;
71 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
72}
73
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080074PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) {
75 return static_cast<PolicyBitmask>(arg);
76}
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080077
Mårten Kongstad02751232018-04-27 13:16:32 +020078} // namespace
79
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010080namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020081
Yurii Zubrytskyi3d13a4f2024-08-05 11:59:27 -070082template <typename T>
83const T* Idmap2Service::GetPointer(const OwningPtr<T>& ptr) {
84 return std::visit([](auto&& ptr) { return ptr.get(); }, ptr);
85}
86
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080087Status Idmap2Service::getIdmapPath(const std::string& overlay_path,
Mårten Kongstad02751232018-04-27 13:16:32 +020088 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
89 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080090 SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_path;
91 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +020092 return ok();
93}
94
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080095Status Idmap2Service::removeIdmap(const std::string& overlay_path, int32_t user_id ATTRIBUTE_UNUSED,
96 bool* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +020097 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080098 SYSTRACE << "Idmap2Service::removeIdmap " << overlay_path;
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010099 const uid_t uid = IPCThreadState::self()->getCallingUid();
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800100 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100101 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
102 *_aidl_return = false;
103 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
104 idmap_path.c_str(), uid));
105 }
Mårten Kongstadb8779022018-11-29 09:53:17 +0100106 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200107 *_aidl_return = false;
108 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
109 }
Mårten Kongstadb8779022018-11-29 09:53:17 +0100110 *_aidl_return = true;
111 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +0200112}
113
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800114Status Idmap2Service::verifyIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800115 const std::string& overlay_name, int32_t fulfilled_policies,
116 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
117 bool* _aidl_return) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800118 SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_path;
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100119 assert(_aidl_return);
Ryan Mitchell038a2842020-06-08 14:41:07 -0700120
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800121 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100122 std::ifstream fin(idmap_path);
123 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
124 fin.close();
hg.choia3a68132020-01-15 17:12:48 +0900125 if (!header) {
126 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800127 LOG(WARNING) << "failed to parse idmap header of '" << idmap_path << "'";
128 return ok();
hg.choia3a68132020-01-15 17:12:48 +0900129 }
130
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800131 const auto target = GetTargetContainer(target_path);
132 if (!target) {
Ryan Mitchella7070132020-05-13 14:17:52 -0700133 *_aidl_return = false;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800134 LOG(WARNING) << "failed to load target '" << target_path << "'";
135 return ok();
136 }
137
138 const auto overlay = OverlayResourceContainer::FromPath(overlay_path);
139 if (!overlay) {
140 *_aidl_return = false;
141 LOG(WARNING) << "failed to load overlay '" << overlay_path << "'";
142 return ok();
Ryan Mitchella7070132020-05-13 14:17:52 -0700143 }
144
145 auto up_to_date =
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800146 header->IsUpToDate(*GetPointer(*target), **overlay, overlay_name,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700147 ConvertAidlArgToPolicyBitmask(fulfilled_policies), enforce_overlayable);
Ryan Mitchella7070132020-05-13 14:17:52 -0700148
Ryan Mitchell038a2842020-06-08 14:41:07 -0700149 *_aidl_return = static_cast<bool>(up_to_date);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800150 if (!up_to_date) {
151 LOG(WARNING) << "idmap '" << idmap_path
152 << "' not up to date : " << up_to_date.GetErrorMessage();
153 }
154 return ok();
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100155}
156
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800157Status Idmap2Service::createIdmap(const std::string& target_path, const std::string& overlay_path,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800158 const std::string& overlay_name, int32_t fulfilled_policies,
159 bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED,
Jooyung Han16bac852020-08-10 12:53:14 +0900160 std::optional<std::string>* _aidl_return) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200161 assert(_aidl_return);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800162 SYSTRACE << "Idmap2Service::createIdmap " << target_path << " " << overlay_path;
Jooyung Han605e39f2020-01-23 13:29:22 +0900163 _aidl_return->reset();
Mårten Kongstad02751232018-04-27 13:16:32 +0200164
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800165 const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies);
166
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800167 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100168 const uid_t uid = IPCThreadState::self()->getCallingUid();
169 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
170 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write accesss",
171 idmap_path.c_str(), uid));
172 }
173
Ryan Mitchellb49cb5e2021-02-10 11:27:29 -0800174 // idmap files are mapped with mmap in libandroidfw. Deleting and recreating the idmap guarantees
175 // that existing memory maps will continue to be valid and unaffected. The file must be deleted
176 // before attempting to create the idmap, so that if idmap creation fails, the overlay will no
177 // longer be usable.
178 unlink(idmap_path.c_str());
179
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800180 const auto target = GetTargetContainer(target_path);
181 if (!target) {
182 return error("failed to load target '%s'" + target_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200183 }
184
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800185 const auto overlay = OverlayResourceContainer::FromPath(overlay_path);
186 if (!overlay) {
187 return error("failed to load apk overlay '%s'" + overlay_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200188 }
189
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800190 const auto idmap = Idmap::FromContainers(*GetPointer(*target), **overlay, overlay_name,
191 policy_bitmask, enforce_overlayable);
Mårten Kongstad02751232018-04-27 13:16:32 +0200192 if (!idmap) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100193 return error(idmap.GetErrorMessage());
Mårten Kongstad02751232018-04-27 13:16:32 +0200194 }
195
Mårten Kongstadb8779022018-11-29 09:53:17 +0100196 umask(kIdmapFilePermissionMask);
Mårten Kongstad02751232018-04-27 13:16:32 +0200197 std::ofstream fout(idmap_path);
198 if (fout.fail()) {
199 return error("failed to open idmap path " + idmap_path);
200 }
Ryan Mitchella9093052020-03-26 17:15:01 -0700201
Mårten Kongstad02751232018-04-27 13:16:32 +0200202 BinaryStreamVisitor visitor(fout);
Mårten Kongstadce424902019-03-01 08:35:37 +0100203 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +0200204 fout.close();
205 if (fout.fail()) {
Ryan Mitchella9093052020-03-26 17:15:01 -0700206 unlink(idmap_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200207 return error("failed to write to idmap path " + idmap_path);
208 }
209
Jooyung Han16bac852020-08-10 12:53:14 +0900210 *_aidl_return = idmap_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200211 return ok();
212}
213
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800214idmap2::Result<Idmap2Service::TargetResourceContainerPtr> Idmap2Service::GetTargetContainer(
215 const std::string& target_path) {
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700216 const bool is_framework = target_path == kFrameworkPath;
Adnan Begovic14e307c12015-07-06 20:06:36 -0700217 const bool is_OmniRomPath = target_path == kOmniRomPath;
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700218 bool use_cache;
219 struct stat st = {};
Adnan Begovic14e307c12015-07-06 20:06:36 -0700220 if (is_framework || is_OmniRomPath || !::stat(target_path.c_str(), &st)) {
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700221 use_cache = true;
222 } else {
223 LOG(WARNING) << "failed to stat target path '" << target_path << "' for the cache";
224 use_cache = false;
225 }
226
227 if (use_cache) {
228 std::lock_guard lock(container_cache_mutex_);
229 if (auto cache_it = container_cache_.find(target_path); cache_it != container_cache_.end()) {
230 const auto& item = cache_it->second;
Adnan Begovic14e307c12015-07-06 20:06:36 -0700231 if (is_framework || is_OmniRomPath ||
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700232 (item.dev == st.st_dev && item.inode == st.st_ino && item.size == st.st_size
233 && item.mtime.tv_sec == st.st_mtim.tv_sec && item.mtime.tv_nsec == st.st_mtim.tv_nsec)) {
Yurii Zubrytskyi3d13a4f2024-08-05 11:59:27 -0700234 return {item.apk};
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800235 }
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700236 container_cache_.erase(cache_it);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800237 }
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800238 }
239
240 auto target = TargetResourceContainer::FromPath(target_path);
241 if (!target) {
242 return target.GetError();
243 }
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700244 if (!use_cache) {
245 return {std::move(*target)};
246 }
247
Yurii Zubrytskyi3d13a4f2024-08-05 11:59:27 -0700248 auto res = std::shared_ptr(std::move(*target));
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700249 std::lock_guard lock(container_cache_mutex_);
250 container_cache_.emplace(target_path, CachedContainer {
251 .dev = dev_t(st.st_dev),
252 .inode = ino_t(st.st_ino),
253 .size = st.st_size,
254 .mtime = st.st_mtim,
Yurii Zubrytskyi3d13a4f2024-08-05 11:59:27 -0700255 .apk = res
Yurii Zubrytskyi70ded192023-05-01 21:58:51 -0700256 });
257 return {res};
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800258}
259
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800260Status Idmap2Service::createFabricatedOverlay(
261 const os::FabricatedOverlayInternal& overlay,
262 std::optional<os::FabricatedOverlayInfo>* _aidl_return) {
263 idmap2::FabricatedOverlay::Builder builder(overlay.packageName, overlay.overlayName,
264 overlay.targetPackageName);
265 if (!overlay.targetOverlayable.empty()) {
266 builder.SetOverlayable(overlay.targetOverlayable);
267 }
268
269 for (const auto& res : overlay.entries) {
Jeremy Meyer05466592022-06-02 21:31:15 +0000270 if (res.dataType == Res_value::TYPE_STRING) {
Jeremy Meyer77c8a932022-08-18 22:06:55 +0000271 builder.SetResourceValue(res.resourceName, res.dataType, res.stringData.value(),
272 res.configuration.value_or(std::string()));
Jeremy Meyera761f332022-10-21 17:42:14 +0000273 } else if (res.binaryData.has_value()) {
274 builder.SetResourceValue(res.resourceName, res.binaryData->get(),
Jeremy Meyerc04bcb32023-07-27 22:49:14 +0000275 res.binaryDataOffset, res.binaryDataSize,
Jeremy Meyerdeb46f312023-11-08 12:20:24 -0800276 res.configuration.value_or(std::string()),
277 res.isNinePatch);
Jeremy Meyer05466592022-06-02 21:31:15 +0000278 } else {
Jeremy Meyer77c8a932022-08-18 22:06:55 +0000279 builder.SetResourceValue(res.resourceName, res.dataType, res.data,
280 res.configuration.value_or(std::string()));
Jeremy Meyer05466592022-06-02 21:31:15 +0000281 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800282 }
283
284 // Generate the file path of the fabricated overlay and ensure it does not collide with an
285 // existing path. Re-registering a fabricated overlay will always result in an updated path.
286 std::string path;
287 std::string file_name;
288 do {
289 constexpr size_t kSuffixLength = 4;
290 const std::string random_suffix = RandomStringForPath(kSuffixLength);
291 file_name = StringPrintf("%s-%s-%s.frro", overlay.packageName.c_str(),
292 overlay.overlayName.c_str(), random_suffix.c_str());
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -0700293 path = StringPrintf("%s/%s", kIdmapCacheDir.data(), file_name.c_str());
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800294
295 // Invoking std::filesystem::exists with a file name greater than 255 characters will cause this
296 // process to abort since the name exceeds the maximum file name size.
297 const size_t kMaxFileNameLength = 255;
298 if (file_name.size() > kMaxFileNameLength) {
299 return error(
300 base::StringPrintf("fabricated overlay file name '%s' longer than %zu characters",
301 file_name.c_str(), kMaxFileNameLength));
302 }
303 } while (std::filesystem::exists(path));
Jeremy Meyera761f332022-10-21 17:42:14 +0000304 builder.setFrroPath(path);
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800305
306 const uid_t uid = IPCThreadState::self()->getCallingUid();
307 if (!UidHasWriteAccessToPath(uid, path)) {
308 return error(base::StringPrintf("will not write to %s: calling uid %d lacks write access",
309 path.c_str(), uid));
310 }
311
Mårten Kongstada384fb72021-05-10 08:34:54 +0000312 const auto frro = builder.Build();
313 if (!frro) {
314 return error(StringPrintf("failed to serialize '%s:%s': %s", overlay.packageName.c_str(),
315 overlay.overlayName.c_str(), frro.GetErrorMessage().c_str()));
316 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800317 // Persist the fabricated overlay.
318 umask(kIdmapFilePermissionMask);
319 std::ofstream fout(path);
320 if (fout.fail()) {
321 return error("failed to open frro path " + path);
322 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800323 auto result = frro->ToBinaryStream(fout);
324 if (!result) {
325 unlink(path.c_str());
326 return error("failed to write to frro path " + path + ": " + result.GetErrorMessage());
327 }
328 if (fout.fail()) {
329 unlink(path.c_str());
330 return error("failed to write to frro path " + path);
331 }
332
333 os::FabricatedOverlayInfo out_info;
334 out_info.packageName = overlay.packageName;
335 out_info.overlayName = overlay.overlayName;
336 out_info.targetPackageName = overlay.targetPackageName;
337 out_info.targetOverlayable = overlay.targetOverlayable;
338 out_info.path = path;
339 *_aidl_return = out_info;
340 return ok();
341}
342
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700343Status Idmap2Service::acquireFabricatedOverlayIterator(int32_t* _aidl_return) {
344 std::lock_guard l(frro_iter_mutex_);
Ryan Mitchellb887f682021-07-15 10:43:42 -0700345 if (frro_iter_.has_value()) {
346 LOG(WARNING) << "active ffro iterator was not previously released";
347 }
348 frro_iter_ = std::filesystem::directory_iterator(kIdmapCacheDir);
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700349 if (frro_iter_id_ == std::numeric_limits<int32_t>::max()) {
350 frro_iter_id_ = 0;
351 } else {
352 ++frro_iter_id_;
353 }
354 *_aidl_return = frro_iter_id_;
Ryan Mitchellb887f682021-07-15 10:43:42 -0700355 return ok();
356}
357
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700358Status Idmap2Service::releaseFabricatedOverlayIterator(int32_t iteratorId) {
359 std::lock_guard l(frro_iter_mutex_);
Ryan Mitchellb887f682021-07-15 10:43:42 -0700360 if (!frro_iter_.has_value()) {
361 LOG(WARNING) << "no active ffro iterator to release";
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700362 } else if (frro_iter_id_ != iteratorId) {
363 LOG(WARNING) << "incorrect iterator id in a call to release";
felkachange03cea42022-08-31 12:43:11 +0800364 } else {
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700365 frro_iter_.reset();
Ryan Mitchellb887f682021-07-15 10:43:42 -0700366 }
367 return ok();
368}
369
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700370Status Idmap2Service::nextFabricatedOverlayInfos(int32_t iteratorId,
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800371 std::vector<os::FabricatedOverlayInfo>* _aidl_return) {
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700372 std::lock_guard l(frro_iter_mutex_);
373
Ryan Mitchellb887f682021-07-15 10:43:42 -0700374 constexpr size_t kMaxEntryCount = 100;
375 if (!frro_iter_.has_value()) {
376 return error("no active frro iterator");
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700377 } else if (frro_iter_id_ != iteratorId) {
378 return error("incorrect iterator id in a call to next");
Ryan Mitchellb887f682021-07-15 10:43:42 -0700379 }
380
381 size_t count = 0;
382 auto& entry_iter = *frro_iter_;
383 auto entry_iter_end = end(*frro_iter_);
384 for (; entry_iter != entry_iter_end && count < kMaxEntryCount; ++entry_iter) {
385 auto& entry = *entry_iter;
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700386 if (!entry.is_regular_file() || !android::IsFabricatedOverlay(entry.path().native())) {
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800387 continue;
388 }
389
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700390 const auto overlay = FabricatedOverlayContainer::FromPath(entry.path().native());
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800391 if (!overlay) {
Ryan Mitchellb887f682021-07-15 10:43:42 -0700392 LOG(WARNING) << "Failed to open '" << entry.path() << "': " << overlay.GetErrorMessage();
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800393 continue;
394 }
395
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700396 auto info = (*overlay)->GetManifestInfo();
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800397 os::FabricatedOverlayInfo out_info;
Yurii Zubrytskyide47d0b2022-09-29 16:43:04 -0700398 out_info.packageName = std::move(info.package_name);
399 out_info.overlayName = std::move(info.name);
400 out_info.targetPackageName = std::move(info.target_package);
401 out_info.targetOverlayable = std::move(info.target_name);
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800402 out_info.path = entry.path();
403 _aidl_return->emplace_back(std::move(out_info));
Ryan Mitchellb887f682021-07-15 10:43:42 -0700404 count++;
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800405 }
Ryan Mitchell6a2ca782021-01-19 13:51:15 -0800406 return ok();
407}
408
409binder::Status Idmap2Service::deleteFabricatedOverlay(const std::string& overlay_path,
410 bool* _aidl_return) {
411 SYSTRACE << "Idmap2Service::deleteFabricatedOverlay " << overlay_path;
412 const uid_t uid = IPCThreadState::self()->getCallingUid();
413
414 if (!UidHasWriteAccessToPath(uid, overlay_path)) {
415 *_aidl_return = false;
416 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
417 overlay_path.c_str(), uid));
418 }
419
420 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
421 if (!UidHasWriteAccessToPath(uid, idmap_path)) {
422 *_aidl_return = false;
423 return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access",
424 idmap_path.c_str(), uid));
425 }
426
427 if (unlink(overlay_path.c_str()) != 0) {
428 *_aidl_return = false;
429 return error("failed to unlink " + overlay_path + ": " + strerror(errno));
430 }
431
432 if (unlink(idmap_path.c_str()) != 0) {
433 *_aidl_return = false;
434 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
435 }
436
437 *_aidl_return = true;
438 return ok();
439}
440
Mårten Kongstad99ae8982021-05-10 11:00:17 +0000441binder::Status Idmap2Service::dumpIdmap(const std::string& overlay_path,
442 std::string* _aidl_return) {
443 assert(_aidl_return);
444
445 const auto idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path);
446 std::ifstream fin(idmap_path);
447 const auto idmap = Idmap::FromBinaryStream(fin);
448 fin.close();
449 if (!idmap) {
450 return error(idmap.GetErrorMessage());
451 }
452
453 std::stringstream stream;
454 PrettyPrintVisitor visitor(stream);
455 (*idmap)->accept(&visitor);
456 *_aidl_return = stream.str();
457
458 return ok();
459}
460
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100461} // namespace android::os