| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* | 
|  | 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 Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2d/Idmap2Service.h" | 
|  | 18 |  | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <sys/stat.h>   // umask | 
|  | 20 | #include <sys/types.h>  // umask | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 21 |  | 
|  | 22 | #include <cerrno> | 
|  | 23 | #include <cstring> | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 24 | #include <filesystem> | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 25 | #include <fstream> | 
|  | 26 | #include <memory> | 
|  | 27 | #include <ostream> | 
|  | 28 | #include <string> | 
| Mårten Kongstad | 1195a6b | 2021-05-11 12:57:01 +0000 | [diff] [blame] | 29 | #include <utility> | 
|  | 30 | #include <vector> | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 31 |  | 
|  | 32 | #include "android-base/macros.h" | 
| Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 33 | #include "android-base/stringprintf.h" | 
| Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 34 | #include "binder/IPCThreadState.h" | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 35 | #include "idmap2/BinaryStreamVisitor.h" | 
|  | 36 | #include "idmap2/FileUtils.h" | 
|  | 37 | #include "idmap2/Idmap.h" | 
| Mårten Kongstad | 99ae898 | 2021-05-10 11:00:17 +0000 | [diff] [blame] | 38 | #include "idmap2/PrettyPrintVisitor.h" | 
| Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 39 | #include "idmap2/Result.h" | 
| Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 40 | #include "idmap2/SysTrace.h" | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 41 |  | 
| Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 42 | using android::base::StringPrintf; | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 43 | using android::binder::Status; | 
|  | 44 | using android::idmap2::BinaryStreamVisitor; | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 45 | using android::idmap2::FabricatedOverlayContainer; | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 46 | using android::idmap2::Idmap; | 
|  | 47 | using android::idmap2::IdmapHeader; | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 48 | using android::idmap2::OverlayResourceContainer; | 
| Mårten Kongstad | 99ae898 | 2021-05-10 11:00:17 +0000 | [diff] [blame] | 49 | using android::idmap2::PrettyPrintVisitor; | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 50 | using android::idmap2::TargetResourceContainer; | 
| Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 51 | using android::idmap2::utils::kIdmapCacheDir; | 
| Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 52 | using android::idmap2::utils::kIdmapFilePermissionMask; | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 53 | using android::idmap2::utils::RandomStringForPath; | 
| Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 54 | using android::idmap2::utils::UidHasWriteAccessToPath; | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 55 |  | 
| Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 56 | using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask; | 
|  | 57 |  | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 58 | namespace { | 
|  | 59 |  | 
| Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame] | 60 | constexpr const char* kFrameworkPath = "/system/framework/framework-res.apk"; | 
|  | 61 |  | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 62 | Status ok() { | 
|  | 63 | return Status::ok(); | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | Status error(const std::string& msg) { | 
|  | 67 | LOG(ERROR) << msg; | 
|  | 68 | return Status::fromExceptionCode(Status::EX_NONE, msg.c_str()); | 
|  | 69 | } | 
|  | 70 |  | 
| Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 71 | PolicyBitmask ConvertAidlArgToPolicyBitmask(int32_t arg) { | 
|  | 72 | return static_cast<PolicyBitmask>(arg); | 
|  | 73 | } | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 74 |  | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 75 | }  // namespace | 
|  | 76 |  | 
| Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 77 | namespace android::os { | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 78 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 79 | Status Idmap2Service::getIdmapPath(const std::string& overlay_path, | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 80 | int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) { | 
|  | 81 | assert(_aidl_return); | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 82 | SYSTRACE << "Idmap2Service::getIdmapPath " << overlay_path; | 
|  | 83 | *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 84 | return ok(); | 
|  | 85 | } | 
|  | 86 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 87 | Status Idmap2Service::removeIdmap(const std::string& overlay_path, int32_t user_id ATTRIBUTE_UNUSED, | 
|  | 88 | bool* _aidl_return) { | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 89 | assert(_aidl_return); | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 90 | SYSTRACE << "Idmap2Service::removeIdmap " << overlay_path; | 
| Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 91 | const uid_t uid = IPCThreadState::self()->getCallingUid(); | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 92 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); | 
| Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 93 | 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 Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 98 | if (unlink(idmap_path.c_str()) != 0) { | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 99 | *_aidl_return = false; | 
|  | 100 | return error("failed to unlink " + idmap_path + ": " + strerror(errno)); | 
|  | 101 | } | 
| Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 102 | *_aidl_return = true; | 
|  | 103 | return ok(); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 106 | Status Idmap2Service::verifyIdmap(const std::string& target_path, const std::string& overlay_path, | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 107 | const std::string& overlay_name, int32_t fulfilled_policies, | 
|  | 108 | bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED, | 
|  | 109 | bool* _aidl_return) { | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 110 | SYSTRACE << "Idmap2Service::verifyIdmap " << overlay_path; | 
| Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 111 | assert(_aidl_return); | 
| Ryan Mitchell | 038a284 | 2020-06-08 14:41:07 -0700 | [diff] [blame] | 112 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 113 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); | 
| Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 114 | std::ifstream fin(idmap_path); | 
|  | 115 | const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin); | 
|  | 116 | fin.close(); | 
| hg.choi | a3a6813 | 2020-01-15 17:12:48 +0900 | [diff] [blame] | 117 | if (!header) { | 
|  | 118 | *_aidl_return = false; | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 119 | LOG(WARNING) << "failed to parse idmap header of '" << idmap_path << "'"; | 
|  | 120 | return ok(); | 
| hg.choi | a3a6813 | 2020-01-15 17:12:48 +0900 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 123 | const auto target = GetTargetContainer(target_path); | 
|  | 124 | if (!target) { | 
| Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 125 | *_aidl_return = false; | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 126 | 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 Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
|  | 137 | auto up_to_date = | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 138 | header->IsUpToDate(*GetPointer(*target), **overlay, overlay_name, | 
| Ryan Mitchell | 038a284 | 2020-06-08 14:41:07 -0700 | [diff] [blame] | 139 | ConvertAidlArgToPolicyBitmask(fulfilled_policies), enforce_overlayable); | 
| Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 140 |  | 
| Ryan Mitchell | 038a284 | 2020-06-08 14:41:07 -0700 | [diff] [blame] | 141 | *_aidl_return = static_cast<bool>(up_to_date); | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 142 | 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 Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 149 | Status Idmap2Service::createIdmap(const std::string& target_path, const std::string& overlay_path, | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 150 | const std::string& overlay_name, int32_t fulfilled_policies, | 
|  | 151 | bool enforce_overlayable, int32_t user_id ATTRIBUTE_UNUSED, | 
| Jooyung Han | 16bac85 | 2020-08-10 12:53:14 +0900 | [diff] [blame] | 152 | std::optional<std::string>* _aidl_return) { | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 153 | assert(_aidl_return); | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 154 | SYSTRACE << "Idmap2Service::createIdmap " << target_path << " " << overlay_path; | 
| Jooyung Han | 605e39f | 2020-01-23 13:29:22 +0900 | [diff] [blame] | 155 | _aidl_return->reset(); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 156 |  | 
| Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 157 | const PolicyBitmask policy_bitmask = ConvertAidlArgToPolicyBitmask(fulfilled_policies); | 
|  | 158 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 159 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); | 
| Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 160 | 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 Mitchell | b49cb5e | 2021-02-10 11:27:29 -0800 | [diff] [blame] | 166 | // 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 Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 172 | const auto target = GetTargetContainer(target_path); | 
|  | 173 | if (!target) { | 
|  | 174 | return error("failed to load target '%s'" + target_path); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 177 | const auto overlay = OverlayResourceContainer::FromPath(overlay_path); | 
|  | 178 | if (!overlay) { | 
|  | 179 | return error("failed to load apk overlay '%s'" + overlay_path); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 182 | const auto idmap = Idmap::FromContainers(*GetPointer(*target), **overlay, overlay_name, | 
|  | 183 | policy_bitmask, enforce_overlayable); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 184 | if (!idmap) { | 
| Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 185 | return error(idmap.GetErrorMessage()); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
| Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 188 | umask(kIdmapFilePermissionMask); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 189 | std::ofstream fout(idmap_path); | 
|  | 190 | if (fout.fail()) { | 
|  | 191 | return error("failed to open idmap path " + idmap_path); | 
|  | 192 | } | 
| Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 193 |  | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 194 | BinaryStreamVisitor visitor(fout); | 
| Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 195 | (*idmap)->accept(&visitor); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 196 | fout.close(); | 
|  | 197 | if (fout.fail()) { | 
| Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 198 | unlink(idmap_path.c_str()); | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 199 | return error("failed to write to idmap path " + idmap_path); | 
|  | 200 | } | 
|  | 201 |  | 
| Jooyung Han | 16bac85 | 2020-08-10 12:53:14 +0900 | [diff] [blame] | 202 | *_aidl_return = idmap_path; | 
| Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 203 | return ok(); | 
|  | 204 | } | 
|  | 205 |  | 
| Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 206 | idmap2::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 Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 227 | Status 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) { | 
|  | 237 | builder.SetResourceValue(res.resourceName, res.dataType, res.data); | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | // Generate the file path of the fabricated overlay and ensure it does not collide with an | 
|  | 241 | // existing path. Re-registering a fabricated overlay will always result in an updated path. | 
|  | 242 | std::string path; | 
|  | 243 | std::string file_name; | 
|  | 244 | do { | 
|  | 245 | constexpr size_t kSuffixLength = 4; | 
|  | 246 | const std::string random_suffix = RandomStringForPath(kSuffixLength); | 
|  | 247 | file_name = StringPrintf("%s-%s-%s.frro", overlay.packageName.c_str(), | 
|  | 248 | overlay.overlayName.c_str(), random_suffix.c_str()); | 
|  | 249 | path = StringPrintf("%s/%s", kIdmapCacheDir, file_name.c_str()); | 
|  | 250 |  | 
|  | 251 | // Invoking std::filesystem::exists with a file name greater than 255 characters will cause this | 
|  | 252 | // process to abort since the name exceeds the maximum file name size. | 
|  | 253 | const size_t kMaxFileNameLength = 255; | 
|  | 254 | if (file_name.size() > kMaxFileNameLength) { | 
|  | 255 | return error( | 
|  | 256 | base::StringPrintf("fabricated overlay file name '%s' longer than %zu characters", | 
|  | 257 | file_name.c_str(), kMaxFileNameLength)); | 
|  | 258 | } | 
|  | 259 | } while (std::filesystem::exists(path)); | 
|  | 260 |  | 
|  | 261 | const uid_t uid = IPCThreadState::self()->getCallingUid(); | 
|  | 262 | if (!UidHasWriteAccessToPath(uid, path)) { | 
|  | 263 | return error(base::StringPrintf("will not write to %s: calling uid %d lacks write access", | 
|  | 264 | path.c_str(), uid)); | 
|  | 265 | } | 
|  | 266 |  | 
| Mårten Kongstad | a384fb7 | 2021-05-10 08:34:54 +0000 | [diff] [blame] | 267 | const auto frro = builder.Build(); | 
|  | 268 | if (!frro) { | 
|  | 269 | return error(StringPrintf("failed to serialize '%s:%s': %s", overlay.packageName.c_str(), | 
|  | 270 | overlay.overlayName.c_str(), frro.GetErrorMessage().c_str())); | 
|  | 271 | } | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 272 | // Persist the fabricated overlay. | 
|  | 273 | umask(kIdmapFilePermissionMask); | 
|  | 274 | std::ofstream fout(path); | 
|  | 275 | if (fout.fail()) { | 
|  | 276 | return error("failed to open frro path " + path); | 
|  | 277 | } | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 278 | auto result = frro->ToBinaryStream(fout); | 
|  | 279 | if (!result) { | 
|  | 280 | unlink(path.c_str()); | 
|  | 281 | return error("failed to write to frro path " + path + ": " + result.GetErrorMessage()); | 
|  | 282 | } | 
|  | 283 | if (fout.fail()) { | 
|  | 284 | unlink(path.c_str()); | 
|  | 285 | return error("failed to write to frro path " + path); | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | os::FabricatedOverlayInfo out_info; | 
|  | 289 | out_info.packageName = overlay.packageName; | 
|  | 290 | out_info.overlayName = overlay.overlayName; | 
|  | 291 | out_info.targetPackageName = overlay.targetPackageName; | 
|  | 292 | out_info.targetOverlayable = overlay.targetOverlayable; | 
|  | 293 | out_info.path = path; | 
|  | 294 | *_aidl_return = out_info; | 
|  | 295 | return ok(); | 
|  | 296 | } | 
|  | 297 |  | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 298 | Status Idmap2Service::acquireFabricatedOverlayIterator() { | 
|  | 299 | if (frro_iter_.has_value()) { | 
|  | 300 | LOG(WARNING) << "active ffro iterator was not previously released"; | 
|  | 301 | } | 
|  | 302 | frro_iter_ = std::filesystem::directory_iterator(kIdmapCacheDir); | 
|  | 303 | return ok(); | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | Status Idmap2Service::releaseFabricatedOverlayIterator() { | 
|  | 307 | if (!frro_iter_.has_value()) { | 
|  | 308 | LOG(WARNING) << "no active ffro iterator to release"; | 
|  | 309 | } | 
|  | 310 | return ok(); | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | Status Idmap2Service::nextFabricatedOverlayInfos( | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 314 | std::vector<os::FabricatedOverlayInfo>* _aidl_return) { | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 315 | constexpr size_t kMaxEntryCount = 100; | 
|  | 316 | if (!frro_iter_.has_value()) { | 
|  | 317 | return error("no active frro iterator"); | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | size_t count = 0; | 
|  | 321 | auto& entry_iter = *frro_iter_; | 
|  | 322 | auto entry_iter_end = end(*frro_iter_); | 
|  | 323 | for (; entry_iter != entry_iter_end && count < kMaxEntryCount; ++entry_iter) { | 
|  | 324 | auto& entry = *entry_iter; | 
|  | 325 | if (!entry.is_regular_file() || !android::IsFabricatedOverlay(entry.path())) { | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 326 | continue; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | const auto overlay = FabricatedOverlayContainer::FromPath(entry.path()); | 
|  | 330 | if (!overlay) { | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 331 | LOG(WARNING) << "Failed to open '" << entry.path() << "': " << overlay.GetErrorMessage(); | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 332 | continue; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | const auto info = (*overlay)->GetManifestInfo(); | 
|  | 336 | os::FabricatedOverlayInfo out_info; | 
|  | 337 | out_info.packageName = info.package_name; | 
|  | 338 | out_info.overlayName = info.name; | 
|  | 339 | out_info.targetPackageName = info.target_package; | 
|  | 340 | out_info.targetOverlayable = info.target_name; | 
|  | 341 | out_info.path = entry.path(); | 
|  | 342 | _aidl_return->emplace_back(std::move(out_info)); | 
| Ryan Mitchell | b887f68 | 2021-07-15 10:43:42 -0700 | [diff] [blame] | 343 | count++; | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 344 | } | 
| Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 345 | return ok(); | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | binder::Status Idmap2Service::deleteFabricatedOverlay(const std::string& overlay_path, | 
|  | 349 | bool* _aidl_return) { | 
|  | 350 | SYSTRACE << "Idmap2Service::deleteFabricatedOverlay " << overlay_path; | 
|  | 351 | const uid_t uid = IPCThreadState::self()->getCallingUid(); | 
|  | 352 |  | 
|  | 353 | if (!UidHasWriteAccessToPath(uid, overlay_path)) { | 
|  | 354 | *_aidl_return = false; | 
|  | 355 | return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access", | 
|  | 356 | overlay_path.c_str(), uid)); | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); | 
|  | 360 | if (!UidHasWriteAccessToPath(uid, idmap_path)) { | 
|  | 361 | *_aidl_return = false; | 
|  | 362 | return error(base::StringPrintf("failed to unlink %s: calling uid %d lacks write access", | 
|  | 363 | idmap_path.c_str(), uid)); | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | if (unlink(overlay_path.c_str()) != 0) { | 
|  | 367 | *_aidl_return = false; | 
|  | 368 | return error("failed to unlink " + overlay_path + ": " + strerror(errno)); | 
|  | 369 | } | 
|  | 370 |  | 
|  | 371 | if (unlink(idmap_path.c_str()) != 0) { | 
|  | 372 | *_aidl_return = false; | 
|  | 373 | return error("failed to unlink " + idmap_path + ": " + strerror(errno)); | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | *_aidl_return = true; | 
|  | 377 | return ok(); | 
|  | 378 | } | 
|  | 379 |  | 
| Mårten Kongstad | 99ae898 | 2021-05-10 11:00:17 +0000 | [diff] [blame] | 380 | binder::Status Idmap2Service::dumpIdmap(const std::string& overlay_path, | 
|  | 381 | std::string* _aidl_return) { | 
|  | 382 | assert(_aidl_return); | 
|  | 383 |  | 
|  | 384 | const auto idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_path); | 
|  | 385 | std::ifstream fin(idmap_path); | 
|  | 386 | const auto idmap = Idmap::FromBinaryStream(fin); | 
|  | 387 | fin.close(); | 
|  | 388 | if (!idmap) { | 
|  | 389 | return error(idmap.GetErrorMessage()); | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | std::stringstream stream; | 
|  | 393 | PrettyPrintVisitor visitor(stream); | 
|  | 394 | (*idmap)->accept(&visitor); | 
|  | 395 | *_aidl_return = stream.str(); | 
|  | 396 |  | 
|  | 397 | return ok(); | 
|  | 398 | } | 
|  | 399 |  | 
| Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 400 | }  // namespace android::os |