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