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