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